I am creating a program that can determine the molecular mass of a chemical formula based on the atomic masses of the elements (respectively). The program won't run because when I am multiplying the elements by its subscript, it is essentially multiplying a string with an integer. How can I convert the elements to their values before multiplying it with the subscripts?
List of elements
M={"H":1.01, "He":4.0, "Li": 6.9, "Be":9.0, "B":10.8, "C":12.0, "N":14.0,
"O":16.0, "F":19.0, "Ne":20.2, "Na":23.0, "Mg":24.3, "Al":27.0, "Si":28.1,
"P":31.0, "S":32.1, "Cl":35.5, "Ar":40.0, "K":39.1, "Ca":40.1, "Sc":45.0,
"Ti":47.9, "V":50.9, "Cr":52.0, "Mn":54.9, "Fe":55.8, "Co":58.9, "Ni": 58.7,
"Cu":63.5, "Zn":65.4, "Ga":69.7, "Ge":72.6, "As":74.9, "Se": 79.0, "Br":
79.9,"Kr":83.8, "Rb":85.5, "Sr":87.6, "Y":88.9, "Zr":91.2, "Nb":92.9,
"Mo":95.9, "Tc":98.0}
#Now we can create our loop to add the molar masses
M=input("What is your first element/symbol")
Z=input("What is the subscript of that element?")
C= M*Z
X= input(str("Do you have another element? Type YES to continue and NO to
stop"))
while X==(str(YES)):
M=input("What is your first element/symbol")
B=input("What is the subscript of that element")
D==M*B
X==input(str("Do you have another element? Type YES to continue and No
to stop"))
else:
print("Your molar mass is", C+D, "g/mol")