Multiplying two elements in a list

Question:

How can I multiply two elements in a list?
I am reading a text file, and printing the following:

for i in range(len(listeisotoper)):
    print("Isotop type:"+listeisotoper[i][0])
    print("Isotopisk masse u: "+listeisotoper[i][1])
    print("Naturlig forekomst: "+listeisotoper[i][2])
    print("xxx"+"g/mol")
    print("n")

However i cannot fathom how i can multiply the listeisotoper[i][1] * listeisotoper[i][2]
and then have it print the number with decimal points.
Any suggestions?

Asked By: Andreas w

||

Answers:

I am assuming your problem is multiplying strings. If so, try this:

for i in range(len(listeisotoper)):
    print("Isotop type:"+listeisotoper[i][0])
    print("Isotopisk masse u: "+listeisotoper[i][1])
    print("Naturlig forekomst: "+listeisotoper[i][2])
    print(str(float(listeisotoper[i][1])*float(listeisotoper[i][2]))+"g/mol")
    print("n")
Answered By: Jon Smith

It isn’t clear question for me, but may be this code will help you

for i in range(len(listeisotoper)):
    print("Isotop type:"+listeisotoper[i][0])
    print("Isotopisk masse u: "+listeisotoper[i][1])
    print("Naturlig forekomst: "+listeisotoper[i][2])
    mult_res = float(listeisotoper[i][1]) * float(listeisotoper[i][2]) 
    print(f"Mult = {mult_res:.1f}")
    print("xxx"+"g/mol")
    print("n")
Answered By: lbnms
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.