-2

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") 
Community
  • 1
  • 1
  • 5
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – DavidG Apr 17 '18 at 22:53
  • you can use `int` like `int('0') => 0` – shahaf Apr 17 '18 at 23:03
  • Just use `Z=int(input("What is the subscript of that element?"))` to convert the string returned by `input()` into an integer. Other types/classes that provide constructors that accept a string argument should also work. – martineau Apr 17 '18 at 23:21

2 Answers2

0

There are inbuilt methods available in python for field conversion like int(value) converting string to int and float(value) converting string to float. You can use them in your case

0

Wrap your input statements in one of the built in conversion functions. ie.

M=str(input("What is your first element/symbol"))
Z=int(input("What is the subscript of that element?"))

This will convert the input from the user to the format you want it. (converting to string isn't actually necessary as that is the default)

Another thing, unrelated to your question, is you are going to want to change the way you are using that dictionary. Perhaps define it as M_dict and then reference it using the M entered by the user like this M_dict[M] That will give you the number corresponding to the element entered by the user which you can then multiply by the subscript.

Stark
  • 1
  • 2
  • Thank you so much! It is also telling me that in my loop, "YES" is not defined and so I can't use it. How do I define this? – C. Programmer Apr 18 '18 at 00:24
  • YES without quotes tells python to expect a variable names YES defined beforehand, what you want is: while X == 'YES': – Stark Apr 18 '18 at 23:34
  • I built a little mass calculator of my own cause I randomly felt like it: https://www.pastebucket.com/566011 – Stark Apr 18 '18 at 23:38