Equation for Density - physics

I am a rookie and learning myself so i dont know where to ask so i wanted to try my luck here
The Equation is p=m/V
while True:
Auswahl = input("Für das beenden des Skripts geben sie Ende ein.\nWas möchten sie berechnen: Dichte, Masse, Volumen: ")
if Auswahl == "Dichte":
print("Geben sie zuerst das Volumen und dann die Masse an")
Volumen = float(input("Volumen: "))
Masse = float(input("Masse: "))
Dichte = Masse/Volumen
print(f"Die Dichte beträgt {Dichte} Einheiten")
elif Auswahl == "Masse":
print("Geben sie zuerst die Dichte und dann das Volumen an")
Dichte = float(input("Dichte: "))
Volumen = float(input("Volumen: "))
Masse = Dichte * Volumen
print(f"Die Masse beträgt {Masse} Einheiten")
elif Auswahl == "Volumen":
print("Geben sie zuerst die Masse und dann die Dichte an")
Masse = float(input("Masse: "))
Dichte = float(input("Dichte: "))
Volumen = Masse / Dichte
print(f"Das Volumen beträgt {Volumen} Einheiten")
elif Auswahl == "Ende":
print(style.RED + "Danke")
quit()
So far its pretty simple since its only 2 variables at a time.
But for this problem:
for average Density
I need multiple variables that differ from task to task, so in some cases i have 5 masses and volumes, sometimes 15 masses and volumes
now to my question how can i tell python to only ask me 15 times and then define 15 variables that i can use for calculating later on
For example = 8 Masses and 8 Volumes
So python should ask me about the 8 different masses and put them into variables and the same goes for volumes
i was thinking about implementing a while loop but quickly found out that i have to define variables by hand which sucks i would like python to do it automatically
i am also open for any improvments
Thank you in advance

i think i solved the problem but i am afraid its not the most efficent way;
gesamt = float(input("Geben Sie die Anzahl der im Körper enthaltenen Stoffe an: "))
b = 1
while b <= gesamt:
b += 1
m_list = []
for i in range(b-1):
m_list.append(float(input("Masse eingeben: ")))
mtotal = 0
for m in m_list:
mtotal = mtotal + m
print(mtotal)
v_list = []
for j in range(b-1):
v_list.append(float(input("Volumen eingeben: ")))
vtotal = 0
for v in v_list:
vtotal = vtotal + v
print(vtotal)
durchschnittliche_Dichte = mtotal/vtotal
print(durchschnittliche_Dichte)

Related

My input function and len function does not return a correct result in some cases

I have a question I wrote this script to format an arabic text to a certain format. But I found a problem then when I type a longer sentence it adds more dots then there are letters. I will paste the code here and explain what the problem is.
import itertools
while True:
input_cloze_deletion = input("\nEnter: text pr 'exit'\n> ")
input_exit_check = input_cloze_deletion.strip().lower()
if input_exit_check == "exit":
break
# copy paste
interpunction_list = ["الله", ",", ".", ":", "?", "!", "'"]
# copy paste
interpunction_list = [ ",", ".", ":", "?", "!", "'", "-", "(", ")", "/", "الله", "اللّٰـه"]
text_replace_0 = input_cloze_deletion.replace(",", " ,")
text_replace_1 = text_replace_0.replace(".", " .")
text_replace_2 = text_replace_1.replace(":", " :")
text_replace_3 = text_replace_2.replace(";", " ;")
text_replace_4 = text_replace_3.replace("?", " ?")
text_replace_5 = text_replace_4.replace("!", " !")
text_replace_6 = text_replace_5.replace("'", " ' ")
text_replace_7 = text_replace_6.replace("-", " - ")
text_replace_8 = text_replace_7.replace("(", " ( ")
text_replace_9 = text_replace_8.replace(")", " ) ")
text_replace_10 = text_replace_9.replace("/", " / ")
text_replace_11 = text_replace_10.replace("الله", "اللّٰـه")
text_split_list = text_replace_11.split()
count_number = []
letter_count_list = []
index_list = itertools.cycle(range(1, 4))
for letter_count in text_split_list:
if letter_count in interpunction_list:
letter_count_list.append(letter_count)
elif "ـ" in letter_count:
letter_count = len(letter_count) - 1
count_number.append(letter_count)
print(letter_count)
else:
letter_count = len(letter_count)
count_number.append(letter_count)
print(letter_count)
for count in count_number:
letter_count_list.append(letter_count * ".")
zip_list = zip(text_split_list, letter_count_list)
zip_list_result = list(zip_list)
for word, count in zip_list_result:
if ((len(word)) == 2 or word == "a" or word == "و") and not word in interpunction_list :
print(f" {{{{c{(next(index_list))}::{word}::{count}}}}}", end="")
elif word and count in interpunction_list:
print(word, end = "")
else:
print(f" {{{{c{(next(index_list))}::{word}::{count}}}}}", end="")
when I type كتب عليـ ـنا و علـ ـي
the return is {{c1::كتب::...}} {{c2::عليـ::...}} {{c3::ـنا::...}} {{c1::و::..}} {{c2::علـ::..}} {{c3::ـي::..}}
but it should be
{{c1::كتب::...}} {{c2::عليـ::...}} {{c3::ـنا::..}} {{c1::و::.}} {{c2::علـ::..}} {{c3::ـي::.}}
I add a print function the print the len() results and the result is correct but it add an extra dot in some case.
But when I type just a single "و" it does a correct len() function but when I input a whole sentence it add an extra dot and I don't know why.
please help

Vectorized alternative to iterrows : Semantic Analysis

Hi I'm currently doing a semantic tweet analysis and want to improve my code running time with Numpy Vectorization.
I tried enhancing my code for a while but was not successful in doing so.
Could I just enter the formula within the loop iteration to a function and apply it via Numpy.vectorize?
ss = SentimentIntensityAnalyzer()
for index, row in tw_list["full_text"].iteritems():
score = ss.polarity_scores(row)
neg = score["neg"]
neu = score["neu"]
pos = score["pos"]
comp = score["compound"]
if neg > pos:
tw_list.loc[index, "sentiment"] = "negative"
elif pos > neg:
tw_list.loc[index, "sentiment"] = "positive"
else:
tw_list.loc[index, "sentiment"] = "neutral"
tw_list.loc[index, "neg"] = neg
tw_list.loc[index, "neu"] = neu
tw_list.loc[index, "pos"] = pos
tw_list.loc[index, "compound"] = comp
Instead of iterating over rows in dataframe, you can make use of apply function.
def get_sentiments(text):
score = ss.polarity_scores(text)
neg = score["neg"]
neu = score["neu"]
pos = score["pos"]
comp = score["compound"]
if neg > pos:
sentiment = "negative"
elif pos > neg:
sentiment = "positive"
else:
sentiment = "neutral"
return sentiment,neg,neu,pos,comp
tw_list[["sentiment","neg","neu","pos","comp"]] = tw_list["full_text"].apply(get_sentiments,result_type='broadcast')
This should give improvement in perfomance

pdfbox keep whitespaces on beginning from line

i have a PDF and i want to get the Text of this PDF with PDFBox 2.x an i want to keep all Whitespaces on the beginning from the Line!
Here the PDF Examples:
First Example with seven Whitespace on the Line-Beginning
when i mark the Text in the PDF and copy it to a Editor i get this:
> Erster Testuebertrag auf die Neuentwicklung fuer die PSA Direktbank ma
> l mit sehr langen Verwendungszweck gleich zum testen wann dieser cuted
EDIT: here is the Stream-Dump from this Sectionimage:
BT
/F19 8.9664 Tf 96.197 606.119 Td [(Kommunikation)]TJ
ET
q
1 0 0 1 85.238 594.35 cm
[]0 d 0 J 0.398 w 0 0 m 0 7.352 l S
Q
BT
/F19 8.9664 Tf 133.856 595.758 Td [(Erster)-600(Testuebertrag)-600(auf)-600(die)-600(Neuentwicklung)-600(fuer)-600(die)-600(PSA)-600(Direktbank)-600(ma)]TJ
ET
q
1 0 0 1 85.238 583.989 cm
[]0 d 0 J 0.398 w 0 0 m 0 7.352 l S
Q
BT
/F19 8.9664 Tf 133.856 585.397 Td [(l)-600(mit)-600(sehr)-600(langen)-600(Verwendungszweck)-600(gleich)-600(zum)-600(testen)-600(wann)-600(dieser)-600(cuted)]TJ
ET
Second Example with five Whitespaces in second Line
when i mark the Text in the PDF and copy it to a Editor i get this:
> Rueckzahlung mal mit Umlauten obwohl das ganze ja gar nicht mehr gehen
> duerte, aber hier haben wir jetzt sogar die zweite Zeile erreicht
EDIT: this is the Stream-Dump of the Sectionimage #2:
BT
/F19 8.9664 Tf 96.197 267.821 Td [(Kommunikation)-8400(:)]TJ
ET
q
1 0 0 1 85.238 256.052 cm
[]0 d 0 J 0.398 w 0 0 m 0 7.352 l S
Q
BT
/F19 8.9664 Tf 117.716 257.46 Td [(Rueckzahlung)-600(mal)-600(mit)-600(Umlauten)-600(obwohl)-600(das)-600(ganze)-600(ja)-600(gar)-600(nicht)-600(mehr)-600(gehen)]TJ
ET
q
1 0 0 1 85.238 245.691 cm
[]0 d 0 J 0.398 w 0 0 m 0 7.352 l S
Q
BT
/F19 8.9664 Tf 123.096 247.099 Td [(duerte,)-600(aber)-600(hier)-600(haben)-600(wir)-600(jetzt)-600(sogar)-600(die)-600(zweite)-600(Zeile)-600(erreicht)]TJ
ET
So, when i extract the Text with
PDFText2HTML Stripper = new PDFText2HTML();
or
PDFTextStripper Stripper = new PDFTextStripper();
the i get every Time the Text without leading Whitespaces on the Line-Beginning but i need it ;)
Example:
> Erster Testuebertrag auf die Neuentwicklung fuer die PSA Direktbank ma
> l mit sehr langen Verwendungszweck gleich zum testen wann dieser cuted
>
> Rueckzahlung mal mit Umlauten obwohl das ganze ja gar nicht mehr gehen
> duerte, aber hier haben wir jetzt sogar die zweite Zeile erreicht
Is there every Solution to keep the Whitespaces with PDFBox 2.x?
Greats

error loops, Gaussian elimination julia

the code below solves a linear system using Gauss method. when I run again, the error occurs:
LoadError: InexactError()
while loading In[176], in expression starting on line 4
in setindex! at array.jl:313
[inlined code] from In[176]:13
in anonymous at no file:12
Running more than 3 times the system is solved. what's up?
A = [4.1 -5 6.8;7.8 -8 -9;-17 4 1];
b = [1,2,3];
x = zeros(3);
m = 0;
al,ac = size(A)
for k= 1:(al-1)
#println("valor de k ",k)
for i = (k+1):al
#println("valor de i ",i)
m = A[i,k]/(A[k,k])
A[i,k] = 0
for j=(k+1):al
#println("valor de j",j)
A[i,j] = A[i,j] - m*A[k,j]
b[i]= b[i] - m*b[k]
end
end
end
x[al] = b[al]/(A[al,al])
for k = (al-1):-1:1
begin
s = 0;
for j = (k+1):al
s = s+A[k,j]*x[j]
end
x[k]=(b[k]-s)/A[k,k]
end
end
println(x)
The error is occurring mainly due to type mismatch between the variable and the value, consider the simple example,
julia> convert(Int64, 0.1)
ERROR: InexactError()
The InexactError() is thrown because it not possible to represent 0.1 as an integer.
julia>convert(Int64, 1.0)
1
As you can see the value 1.0 does not have problems being converted to Int64. So in your case since the type for array b is Array{Int64,1} and it sure cant hold floating point variables in due process and hence the error.
Considering #jeff 's inputs, it might be better to pakckthe code in a function,
julia>function f(A::Array{Float64,2}, b::Vector{Float64})
x = zeros(3);
m = 0.0;
al,ac = size(A)
for k= 1:(al-1)
#println("valor de k ",k)
for i = (k+1):al
#println("valor de i ",i)
m = A[i,k]/(A[k,k])
A[i,k] = 0
for j=(k+1):al
#println("valor de j",j)
A[i,j] = A[i,j] - m*A[k,j]
b[i]= b[i] - m*b[k]
end
end
end
x[al] = b[al]/(A[al,al])
for k = (al-1):-1:1
begin
s = 0;
for j = (k+1):al
s = s+A[k,j]*x[j]
end
x[k]=(b[k]-s)/A[k,k]
end
end
return x, A, b
end
f (generic function with 1 method)
julia> A = [4.1 -5 6.8;7.8 -8 -9;-17 4 1];
julia> b = [1,2,3.0];
This should result in the following transformation of matrix A into a upper triangular form,
julia> A
3×3 Array{Float64,2}:
4.1 -5.0 6.8
0.0 1.5122 -21.9366
0.0 0.0 -213.523

Group by Pairs all textbox input vb.net 2010

I have a textbox, I want to group all input two by two.
Input: E5D3DFOXJFUIOXZJDFCNIUEBSKDLFJCNESODFKJ
I want to become: E5 D3 DF OX JF UI OX ZJ DF CN IU EB SK DL FJ CN ES OD FK J
How can I do that?
I have this function but it doesn't really work :
For i As Integer = TextBox1.Text.Length - 2 To 2 Step -2
TextBox1.Text = TextBox1.Text.Insert(i, " ")
Next
It gives me something like that :
E5D 3D FO XJ FU IO XZ JD FC NI UE BS KD LF JC NE SO DF KJ
or when the string is too long it's like that :
E5D 3D FO XJ FU IO XZ JD F C NI UE BS K D LF JC NE SO DF KJ
Anyone can help me with this?
Please excuse the language switch to C#. Not particularly elegant, but the following code should work for both even and odd lengths of string
string buffer = String.Empty;
for (int i = 0; i < textBox1.Text.Length; i += 2)
{
// Exclude the case where 1 or 2 remaining chars here (no trailing space)
if (textBox1.Text.Length - i > 2)
{
buffer += textBox1.Text.Substring(i, 2) + " ";
}
else
{
buffer += textBox1.Text.Substring(i);
}
}
textBox1.Text = buffer;
Thanls you for reply, C# or VB.net it's the same :-) in vb2010 it will be :
Dim buffer As String = [String].Empty
For i As Integer = 0 To TextBox1.Text.Length - 1 Step 2
' Exclude the case where 1 or 2 remaining chars here (no trailing space)
If TextBox1.Text.Length - i > 2 Then
buffer += TextBox1.Text.Substring(i, 2) & " "
Else
buffer += TextBox1.Text.Substring(i)
End If
Next
TextBox1.Text = buffer
K.N.A.82.A.C.M