Compositions of Compounds Calculator


#1

Here is my second project! Still pretty new to python so please give me some good feedback! Thanks.

#Element Symbol and Atomic Weight:
none = 0
H	= 1.00784
He	= 4.002602
Li	= 6.938
Be	= 9.0121831
B	= 10.806
C	= 12.0096
N	= 14.00643
O	= 15.99903
F	= 18.99840316
Ne	= 20.1797
Na	= 22.98976928
Mg	= 24.304
Al	= 26.9815385
Si	= 28.084
P	= 30.973762
S	= 32.059
Cl	= 35.446
Ar	= 39.948
K	= 39.0983
Ca	= 40.078
Sc	= 44.955908
Ti	= 47.867
V	= 50.9415
Cr	= 51.9961
Mn	= 54.938044
Fe	= 55.845
Co	= 58.933194
Ni	= 58.6934
Cu	= 63.546
Zn	= 65.38
Ga	= 69.723
Ge	= 72.63
As	= 74.921595
Se	= 78.971
Br	= 79.901
Kr	= 83.798
Rb	= 85.4678
Sr	= 87.62
Y	= 88.90594
Zr	= 91.224
Nb	= 92.90637
Mo	= 95.95
Tc	= -97
Ru	= 101.07
Rh	= 102.9055
Pd	= 106.42
Ag	= 107.8682
Cd	= 112.414
In	= 114.818
Sn	= 118.71
Sb	= 121.76
Te	= 127.6
I	= 126.90447
Xe	= 131.293
Cs	= 132.905452
Ba	= 137.327
La	= 138.90547
Ce	= 140.116
Pr	= 140.90766
Nd	= 144.242
Pm	= -145
Sm	= 150.36
Eu	= 151.964
Gd	= 157.25
Tb	= 158.92535
Dy	= 162.5
Ho	= 164.93033
Er	= 167.259
Tm	= 168.93422
Yb	= 173.045
Lu	= 174.9668
Hf	= 178.49
Ta	= 180.94788
W	= 183.84
Re	= 186.207
Os	= 190.23
Ir	= 192.217
Pt	= 195.084
Au	= 196.966569
Hg	= 200.592
Tl	= 204.382
Pb	= 207.2
Bi	= 208.9804
Po	= -209
At	= -210
Rn	= -222
Fr	= -223
Ra	= -226
Ac	= -227
Th	= 232.0377
Pa	= 231.03588
U	= 238.02891
Np	= -237
Pu	= -244
Am	= -243
Cm	= -247
Bk	= -247
Cf	= -251
Es	= -252
Fm	= -257
Md	= -258
No	= -259
Lr	= -262
Rf	= -263
Db	= -268
Sg	= -271
Bh	= -270
Hs	= -270
Mt	= -278
Ds	= -281
Rg	= -281
Cn	= -285
Uut	= -286
Fl	= -289
Uup	= -289
Lv	= -293
Uus	= -294
Uuo	= -294

def composition_of_compounds(element1,element2,element3):
    #Molar_Mass:
    if len(element1) == 2 and len(element2) == 2 and len(element3) == 2: #checking to make sure it is basic outline for compound
        molar_info_element1 = element1[0] * element1[1] #finding the molar mass of the fist element
        molar_info_element2 = element2[0] * element2[1] #finding the molar mass of the second element
        molar_info_element3 = element3[0] * element3[1] #finding the molar mass of the third element
        molar_mass = molar_info_element1 + molar_info_element2 + molar_info_element3 #adding up the molar mass of each element into the compound's molar mass
        #Percent Composition:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100 #finding the percentage of the first element
        percent_composition_element2 = (molar_info_element2 / molar_mass) * 100 #finding the percentage of the second element
        percent_composition_element3 = (molar_info_element3 / molar_mass) * 100 #finding the percentage of the third element
        print("") #for easier output reading
        print("Molar Mass of Compound:") #letting you know in the output that the number below is the molar mass
        print(molar_mass) #printing the molar mass
        print("---------------------------") #for easier output reading
        print("Element 1's percent:") #letting you know in the output that the number below is the percentage of element 1 in the compound
        print(str(percent_composition_element1) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 2's percent:") #letting you know in the output that the number below is the percentage of element 2 in the compound
        print(str(percent_composition_element2) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 3's percent:") #letting you know in the output that the number below is the percentage of element 3 in the compound
        print(str(percent_composition_element3) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("") #for easier output reading
    elif len(element1) == 6 and len(element2) == 2 and len(element3) == 2: #checking to make sure it has element 1 be a multi-compound
        #Molar Mass:
        molar_info_element1 = (element1[0] * element1[1]) + (element1[2] * element1[3]) + (element1[4] + element1[5]) #finding the molar mass of the first element compound
        molar_info_element2 = element2[0] * element2[1] #finding the molar mass of the second element
        molar_info_element3 = element3[0] * element3[1] #finding the molar mass of the third element
        molar_mass = molar_info_element1 + molar_info_element2 + molar_info_element3 #adding up the molar mass of each element into the compound's molar mass
        #Percent Composition:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100 #finding the percentage of the first element compound
        percent_composition_element2 = (molar_info_element2 / molar_mass) * 100 #finding the percentage of the second element
        percent_composition_element3 = (molar_info_element3 / molar_mass) * 100 #finding the percentage of the third element
        print("") #for easier output reading
        print("Molar Mass of Compound:") #letting you know in the output that the number below is the molar mass
        print(molar_mass) #printing the molar mass
        print("---------------------------") #for easier output reading
        print("Element 1's percent:") #letting you know in the output that the number below is the percentage of element 1 in the compound
        print(str(percent_composition_element1) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 2's percent:") #letting you know in the output that the number below is the percentage of element 2 in the compound
        print(str(percent_composition_element2) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 3's percent:") #letting you know in the output that the number below is the percentage of element 3 in the compound
        print(str(percent_composition_element3) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("") #for easier output reading
    elif len(element1) == 2 and len(element2) == 6 and len(element3) == 2: #checking to make sure it has element 2 be a multi-compound
        #Molar Mass:
        molar_info_element1 = element1[0] * element1[1] #finding the molar mass of the fist element
        molar_info_element2 = (element2[0] * element2[1]) + (element2[2] * element2[3]) + (element2[4] + element2[5]) #finding the molar mass of the second element compound
        molar_info_element3 = element3[0] * element3[1] #finding the molar mass of the third element
        molar_mass = molar_info_element1 + molar_info_element2 + molar_info_element3 #adding up the molar mass of each element into the compound's molar mass
        #Percent Composition:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100 #finding the percentage of the first element
        percent_composition_element2 = (molar_info_element2 / molar_mass) * 100 #finding the percentage of the second element compound
        percent_composition_element3 = (molar_info_element3 / molar_mass) * 100 #finding the percentage of the third element
        print("") #for easier output reading
        print("Molar Mass of Compound:") #letting you know in the output that the number below is the molar mass
        print(molar_mass) #printing the molar mass
        print("---------------------------") #for easier output reading
        print("Element 1's percent:") #letting you know in the output that the number below is the percentage of element 1 in the compound
        print(str(percent_composition_element1) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 2's percent:") #letting you know in the output that the number below is the percentage of element 2 in the compound
        print(str(percent_composition_element2) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 3's percent:") #letting you know in the output that the number below is the percentage of element 3 in the compound
        print(str(percent_composition_element3) + "%") #printing the composition percentage of that element in the compound #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("") #for easier output reading
    elif len(element1) == 2 and len(element2) == 2 and len(element3) == 6: #checking to make sure it has element 3 be a multi-compound
        #Molar Mass:
        molar_info_element1 = element1[0] * element1[1] #finding the molar mass of the fist element
        molar_info_element2 = element2[0] * element2[1] #finding the molar mass of the second element
        molar_info_element3 = (element3[0] * element3[1]) + (element3[2] * element3[3]) + (element3[4] + element3[5]) #finding the molar mass of the third element compound
        #Percent Composition:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100 #finding the percentage of the first element
        percent_composition_element2 = (molar_info_element2 / molar_mass) * 100 #finding the percentage of the second element
        percent_composition_element3 = (molar_info_element3 / molar_mass) * 100 #finding the percentage of the third element compound
        print("") #for easier output reading
        print("Molar Mass of Compound:") #letting you know in the output that the number below is the molar mass
        print(molar_mass) #printing the molar mass
        print("---------------------------") #for easier output reading
        print("Element 1's percent:") #letting you know in the output that the number below is the percentage of element 1 in the compound
        print(str(percent_composition_element1) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 2's percent:") #letting you know in the output that the number below is the percentage of element 2 in the compound
        print(str(percent_composition_element2) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 3's percent:") #letting you know in the output that the number below is the percentage of element 3 in the compound
        print(str(percent_composition_element3) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("") #for easier output reading
    elif len(element1) == 7 and len(element2) == 2 and len(element3) == 2: #checking to make sure it has element 1 be a multi-compound being multiplied
        #Molar Mass:
        molar_info_element1 = ((element1[0] * element1[1]) + (element1[2] * element1[3]) + (element1[4] + element1[5])) * element1[6] #finding the molar mass of the first element that is a multicompound that is multiplied
        molar_info_element2 = element2[0] * element2[1] #finding the molar mass of the second element
        molar_info_element3 = element3[0] * element3[1]
        molar_mass = molar_info_element1 + molar_info_element2 + molar_info_element3 #adding up the molar mass of each element into the compound's molar mass
        #Percent Composition:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100 #finding the percentage of the first element compound
        percent_composition_element2 = (molar_info_element2 / molar_mass) * 100 #finding the percentage of the second element
        percent_composition_element3 = (molar_info_element3 / molar_mass) * 100 #finding the percentage of the third element
        print("") #for easier output reading
        print("Molar Mass of Compound:") #letting you know in the output that the number below is the molar mass
        print(molar_mass) #printing the molar mass
        print("---------------------------") #for easier output reading
        print("Element 1's percent:") #letting you know in the output that the number below is the percentage of element 1 in the compound
        print(str(percent_composition_element1) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 2's percent:") #letting you know in the output that the number below is the percentage of element 2 in the compound
        print(str(percent_composition_element2) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 3's percent:") #letting you know in the output that the number below is the percentage of element 3 in the compound
        print(str(percent_composition_element3) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("") #for easier output reading
    elif len(element1) == 2 and len(element2) == 7 and len(element3) == 2: #checking to make sure it has element 2 be a multi-compound being multiplied
        #Molar Mass:
        molar_info_element1 = element1[0] * element1[1] #finding the molar mass of the fist element
        molar_info_element2 = ((element2[0] * element2[1]) + (element2[2] * element2[3]) + (element2[4] + element2[5])) * element2[6] #finding the molar mass of the second element that is a multicompound that is multiplied
        molar_info_element3 = element3[0] * element3[1]
        molar_mass = molar_info_element1 + molar_info_element2 + molar_info_element3 #adding up the molar mass of each element into the compound's molar mass
        #Percent Composition:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100 #finding the percentage of the first element
        percent_composition_element2 = (molar_info_element2 / molar_mass) * 100 #finding the percentage of the second element compound
        percent_composition_element3 = (molar_info_element3 / molar_mass) * 100
        print("") #for easier output reading
        print("Molar Mass of Compound:") #letting you know in the output that the number below is the molar mass
        print(molar_mass) #printing the molar mass
        print("---------------------------") #for easier output reading
        print("Element 1's percent:") #letting you know in the output that the number below is the percentage of element 1 in the compound
        print(str(percent_composition_element1) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 2's percent:") #letting you know in the output that the number below is the percentage of element 2 in the compound
        print(str(percent_composition_element2) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 3's percent:") #letting you know in the output that the number below is the percentage of element 3 in the compound
        print(str(percent_composition_element3) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("") #for easier output reading
    elif len(element1) == 2 and len(element2) == 2 and len(element3) == 7: #checking to make sure it has element 3 be a multi-compound being multiplied
        #Molar Mass:
        molar_info_element1 = element1[0] * element1[1] #finding the molar mass of the fist element
        molar_info_element2 = element2[0] * element2[1] #finding the molar mass of the second element
        molar_info_element3 = ((element3[0] * element3[1]) + (element3[2] * element3[3]) + (element3[4] + element3[5])) * element3[7] #finding the molar mass of the third element that is a multicompound that is multiplied
        molar_mass = molar_info_element1 + molar_info_element2 + molar_info_element3 #adding up the molar mass of each element into the compound's molar mass
        #Percent Composition:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100 #finding the percentage of the first element
        percent_composition_element2 = (molar_info_element2 / molar_mass) * 100 #finding the percentage of the second element
        percent_composition_element3 = (molar_info_element3 / molar_mass) * 100
        print("") #for easier output reading
        print("Molar Mass of Compound:") #letting you know in the output that the number below is the molar mass
        print(molar_mass) #printing the molar mass
        print("---------------------------") #for easier output reading
        print("Element 1's percent:") #letting you know in the output that the number below is the percentage of element 1 in the compound
        print(str(percent_composition_element1) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 2's percent:") #letting you know in the output that the number below is the percentage of element 2 in the compound
        print(str(percent_composition_element2) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("Element 3's percent:") #letting you know in the output that the number below is the percentage of element 3 in the compound
        print(str(percent_composition_element3) + "%") #printing the composition percentage of that element in the compound
        print("---------------------------") #for easier output reading
        print("") #for easier output reading
    else: #checking if the function inputs are wrong
        print("There is a problem! Check your function inputs")

#Examples:
composition_of_compounds([Ag,1,Na,2,H,4,],[F,1],[O,0])
composition_of_compounds([Ag,1,Na,2,H,4,2],[F,1],[O,0])
composition_of_compounds([Na,3],[P,1],[O,4])

#2

Is there a particular reason why everything is defined as a variable? Would a dictionary help to build order and reduce the variable load?

the_elements = {
  'H' : 1.00784,
  'He': 4.002602,
  ...
}

Not sure I understand why some of the atomic weights are shown as negatives. Not a chemist so excuse my ignorance. Please explain.


#3

I haven’t learned how to do dictionaries yet. Also they are supposed to be positive I fixed that.


#4

Are you familiar with JavaScript Objects? Ever heard of an associative array? The term describes a data structure consisting of key->value pairs, where the value is associated with a unique key. Values may repeat, but keys do not.

To access a value in a dictionary, we use the key as a subscript…

print(the_elements['He'])    # 4.002602

Dictionaries are not normally ordered, but since they have unique keys they make excellent lookup tables. I won’t get ahead of your studies, but would recommend setting this project to the side while you brush up on the basics of data structures.

In the meantime, I’ve constructed a dictionary of all the elements for you to practice with…

the_elements
the_elements = {
  'none' : 0,
  'H'  : 1.00784,
  'He' : 4.002602,
  'Li' : 6.938,
  'Be' : 9.0121831,
  'B'  : 10.806,
  'C'  : 12.0096,
  'N'  : 14.00643,
  'O'  : 15.99903,
  'F'  : 18.99840316,
  'Ne' : 20.1797,
  'Na' : 22.98976928,
  'Mg' : 24.304,
  'Al' : 26.9815385,
  'Si' : 28.084,
  'P'  : 30.973762,
  'S'  : 32.059,
  'Cl' : 35.446,
  'Ar' : 39.948,
  'K'  : 39.0983,
  'Ca' : 40.078,
  'Sc' : 44.955908,
  'Ti' : 47.867,
  'V'  : 50.9415,
  'Cr' : 51.9961,
  'Mn' : 54.938044,
  'Fe' : 55.845,
  'Co' : 58.933194,
  'Ni' : 58.6934,
  'Cu' : 63.546,
  'Zn' : 65.38,
  'Ga' : 69.723,
  'Ge' : 72.63,
  'As' : 74.921595,
  'Se' : 78.971,
  'Br' : 79.901,
  'Kr' : 83.798,
  'Rb' : 85.4678,
  'Sr' : 87.62,
  'Y'  : 88.90594,
  'Zr' : 91.224,
  'Nb' : 92.90637,
  'Mo' : 95.95,
  'Tc' : 97,
  'Ru' : 101.07,
  'Rh' : 102.9055,
  'Pd' : 106.42,
  'Ag' : 107.8682,
  'Cd' : 112.414,
  'In' : 114.818,
  'Sn' : 118.71,
  'Sb' : 121.76,
  'Te' : 127.6,
  'I'  : 126.90447,
  'Xe' : 131.293,
  'Cs' : 132.905452,
  'Ba' : 137.327,
  'La' : 138.90547,
  'Ce' : 140.116,
  'Pr' : 140.90766,
  'Nd' : 144.242,
  'Pm' : 145,
  'Sm' : 150.36,
  'Eu' : 151.964,
  'Gd' : 157.25,
  'Tb' : 158.92535,
  'Dy' : 162.5,
  'Ho' : 164.93033,
  'Er' : 167.259,
  'Tm' : 168.93422,
  'Yb' : 173.045,
  'Lu' : 174.9668,
  'Hf' : 178.49,
  'Ta' : 180.94788,
  'W'  : 183.84,
  'Re' : 186.207,
  'Os' : 190.23,
  'Ir' : 192.217,
  'Pt' : 195.084,
  'Au' : 196.966569,
  'Hg' : 200.592,
  'Tl' : 204.382,
  'Pb' : 207.2,
  'Bi' : 208.9804,
  'Po' : 209,
  'At' : 210,
  'Rn' : 222,
  'Fr' : 223,
  'Ra' : 226,
  'Ac' : 227,
  'Th' : 232.0377,
  'Pa' : 231.03588,
  'U'  : 238.02891,
  'Np' : 237,
  'Pu' : 244,
  'Am' : 243,
  'Cm' : 247,
  'Bk' : 247,
  'Cf' : 251,
  'Es' : 252,
  'Fm' : 257,
  'Md' : 258,
  'No' : 259,
  'Lr' : 262,
  'Rf' : 263,
  'Db' : 268,
  'Sg' : 271,
  'Bh' : 270,
  'Hs' : 270,
  'Mt' : 278,
  'Ds' : 281,
  'Rg' : 281,
  'Cn' : 285,
  'Uut': 286,
  'Fl' : 289,
  'Uup': 289,
  'Lv' : 293,
  'Uus': 294,
  'Uuo': 294
}

Something to note concerning syntax…

my_dict = {
  key_str : any_type,
}

It’s imperative that Python keys be written in quotes. The value may be any data type, including data structure. Take for instance the above dictionary expanded to include additional information…

the_elements = {
  'H'  : {'name': 'Hydrogen', 'atomic_weight': 1.00784},
  'He' : {'name': 'Helium', 'atomic_weight': 4.002602},
  ...
}
print (the_elements['He'].name)             # Helium
print (the_elements['He'].atomic_weight)    # 4.002602

#5

Awesome, I will share with you the version that I make with that in it once I finish.


#6

So I was editing the file and I am having a problem with a Syntax error that I am getting. Here is the new code that I am working on:

#Element Symbol and Atomic Weight:
elements = {
"none": "None", "atomic_weight": 0,
"H":  {"name": "Hydrogen", "atomic_weight": 1.00784},
"He": {"name": "Helium", "atomic_weight": 4.002602},
"Li": {"name": "Lithium", "atomic_weight": 6.938,
"Be": {"name": "Beryllium", "atomic_weight": 9.0121831,
"C":  {"name": "Carbon", "atomic_weight": 12.0096,
"O":  {"name": "Oxygen", "atomic_weight": 15.99903,
"N":  {"name": "Nitrogen", "atomic_weight": 14.00643,
"F":  {"name": "Fluorine", "atomic_weight": 18.99840316,
"Ne": {"name": "Neon", "atomic_weight": 20.1797,
"Na": {"name": "Sodium", "atomic_weight": 22.98976928,
"Mg": {"name": "Magnesium", "atomic_weight": 24.304,
"Al": {"name": "Aluminium", "atomic_weight": 26.9815385,
"Si": {"name": "Silicon", "atomic_weight": 28.084,
"P":  {"name": "Phosphorus", "atomic_weight": 30.973762,
"S":  {"name": "Sulfur", "atomic_weight": 32.059,
"Cl": {"name": "Chlorine", "atomic_weight": 35.446,
"Ar": {"name": "Argon", "atomic_weight": 39.948},
"K":  {"name": "Potassium", "atomic_weight": 39.0983},
"Ca": {"name": "Calcium", "atomic_weight": 40.078},
"Sc": {"name": "Scandium", "atomic_weight": 44.955908},
"Ti": {"name": "Titanium", "atomic_weight": 47.867},
"V":  {"name": "Vanadium", "atomic_weight": 50.9415},
"Cr": {"name": "Chromium", "atomic_weight": 51.9961},
"Mn": {"name": "Manganese", "atomic_weight": 54.938044},
"Fe": {"name": "Iron", "atomic_weight": 55.845},
"Co": {"name": "Cobalt", "atomic_weight": 58.933194},
"Ni": {"name": "Nickel", "atomic_weight": 58.6934},
"Cu": {"name": "Copper", "atomic_weight": 63.546},
"Zn": {"name": "Zinc", "atomic_weight": 65.38},
"Ga": {"name": "Gallium", "atomic_weight": 69.723},
"Ge": {"name": "Germanium", "atomic_weight": 72.63},
"As": {"name": "Arsenic", "atomic_weight": 74.921595},
"Se": {"name": "Selenium", "atomic_weight": 78.971},
"Br": {"name": "Bromine", "atomic_weight": 79.901},
"Kr": {"name": "Krypton", "atomic_weight": 83.798},
"Rb": {"name": "Rubidium", "atomic_weight": 85.4678},
"Sr": {"name": "Strontium", "atomic_weight": 87.62},
"Y":  {"name": "Yttrium", "atomic_weight": 88.90594},
"Zr": {"name": "Zirconium", "atomic_weight": 91.224},
"Nb": {"name": "Niobium", "atomic_weight": 92.90637},
"Mo": {"name": "Molybdenum", "atomic_weight": 95.95},
"Tc": {"name": "Technetium", "atomic_weight": 97},
"Ru": {"name": "Ruthenium", "atomic_weight": 101.07},
"Rh": {"name": "Rhodium", "atomic_weight": 102.9055},
"Pd": {"name": "Palladium", "atomic_weight": 106.42},
"Ag": {"name": "Silver", "atomic_weight": 107.8682},
"Cd": {"name": "Cadmium", "atomic_weight": 107.8682},
"In": {"name": "Indium", "atomic_weight": 114.818},
"Sn": {"name": "Tin", "atomic_weight": 118.71},
"Sb": {"name": "Antimony", "atomic_weight": 121.76},
"Te": {"name": "Tellurium", "atomic_weight": 127.6},
"I":  {"name": "Iodine", "atomic_weight": 126.90447},
"Xe": {"name": "Xenon", "atomic_weight": 131.293},
"Cs": {"name": "Caesium", "atomic_weight": 132.905452},
"Ba": {"name": "Barium", "atomic_weight": 137.327},
"La": {"name": "Lanthanum", "atomic_weight": 138.90547},
"Ce": {"name": "Cerium", "atomic_weight": 140.116},
"Pr": {"name": "Praseodymium", "atomic_weight": 140.90766},
"Nd": {"name": "Neodymium", "atomic_weight": 144.242},
"Pm": {"name": "Promethium", "atomic_weight": 145},
"Sm": {"name": "Samarium", "atomic_weight": 150.36},
"Eu": {"name": "Europium", "atomic_weight": 151.964},
"Gd": {"name": "Gadolinium", "atomic_weight": 157.25},
"Tb": {"name": "Terbium", "atomic_weight": 158.92535},
"Dy": {"name": "Dysprosium", "atomic_weight": 162.5},
"Ho": {"name": "Holmium", "atomic_weight": 164.93033},
"Er": {"name": "Erbium", "atomic_weight": 167.259},
"Tm": {"name": "Thulium", "atomic_weight": 168.93422},
"Yb": {"name": "Ytterbium", "atomic_weight": 173.045},
"Lu": {"name": "Lutetium", "atomic_weight": 174.9668},
"Hf": {"name": "Hafnium", "atomic_weight": 178.49},
"Ta": {"name": "Tantalum", "atomic_weight": 180.94788},
"W":  {"name": "Tungsten", "atomic_weigh": 183.84},
"Re": {"name": "Rhenium", "atomic_weight": 186.207},
"Os": {"name": "Osmium", "atomic_weight": 190.23},
"Ir": {"name": "Iridium", "atomic_weight": 192.217},
"Pt": {"name": "Platinum", "atomic_weight": 195.084},
"Au": {"name": "Gold", "atomic_weight": 196.966569},
"Hg": {"name": "Mercury", "atomic_weight": 200.592},
"Tl": {"name": "Thalium", "atomic_weight": 204.382},
"Pb": {"name": "Lead", "atomic_weight": 207.2},
"Bi": {"name": "Bismuth", "atomic_weight": 208.9804},
"Po": {"name": "Polonium", "atomic_weight": 209},
"At": {"name": "Astatine", "atomic_weight": 210},
"Rn": {"name": "Radon", "atomic_weight": 222},
"Fr": {"name": "Francium", "atomic_weight": 223},
"Ra": {"name": "Radium", "atomic_weight": 226},
"Ac": {"name": "Actinium", "atomic_weight": 227},
"Th": {"name": "Thorium", "atomic_weight": 232.0377},
"Pa": {"name": "Protactinium", "atomic_weight": 231.03588},
"U":  {"name": "Uranium", "atomic_weight": 238.02891},
"Np": {"name": "Neptunium", "atomic_weight": 237},
"Pu": {"name": "Plutonium", "atomic_weight": 244},
"Am": {"name": "Americium", "atomic_weight": 243},
"Cm": {"name": "Curium", "atomic_weight": 247},
"Bk": {"name": "Berkelium", "atomic_weight": 247},
"Cf": {"name": "Californium", "atomic_weight": 251},
"Es": {"name": "Einsteinium", "atomic_weight": 252},
"Fm": {"name": "Fermium", "atomic_weight_": 257},
"Md": {"name": "Mendelevium", "atomic_weight": 258},
"No": {"name": "Nobelium", "atomic_weight": 259},
"Lr": {"name": "Lawrencium", "atomic_weight": 262},
"Rf": {"name": "Rutherfordium", "atomic_weight": 263},
"Db": {"name": "Dubnium", "atomic_weight": 268},
"Sg": {"name": "Seaborgium", "atomic_weight": 271},
"Bh": {"name": "Bohrium", "atomic_weight": 270},
"Hs": {"name": "Hassium", "atomic_weight": 270},
"Mt": {"name": "Meitnerium", "atomic_weight": 278},
"Ds": {"name": "Darmstadtium", "atomic_weight": 281},
"Rg": {"name": "Roentgenium", "atomic_weight": 281},
"Cn": {"name": "Copernicium", "atomic_weight": 285},
"Ni": {"name": "Nihonium", "atomic_weight": 286},
"Fl": {"name": "Flerovium", "atomic_weight": 289},
"Mc": {"name": "Moscovium", "atomic_weight": 289},
"Lv": {"name": "Livermorium", "atomic_weight": 293},
"Ts": {"name": "Tennessine", "atomic_weight": 294},
"Og": {"name": "Organesson", "atomic_weight": 294},
}


def composition_of_compounds_calculator(element1,element2,element3):
    if len(element1) == 2 and len(element2 == 2 and len(element3) == 2):
        #Molar Mass Calculations:
        molar_info_element1 = elements[element1[0]].atomic_weight * element1[1]
        molar_info_element2 = elements[element2[0]].atomic_weight * element2[1]
        molar_info_element3 = elements[element3[0]].atomic_weight * element3[1]
        molar_mass = molar_info_element1 + molar_info_element2 + molar_info_element3
        #Percent Composition Calculations:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100
        percent_composition_element2 = (molar_info_element1 / molar_mass) * 100
        percent_composition_element3 = (molar_info_element1 / molar_mass) * 100
        #Print_Statements:
        print("")
        print("Molar Mass of compound:")
        print(molar_mass)
    return molar_info_element1

#7

Whatzat?! This attribute will need an instance object of its own, just like the rest.

"none": {"name": "None", "atomic_weight": 0},

There are a number of missing }'s in the code above (Lithium thru Chlorine). Is it the same on your code? That needs to be fixed.


#8

Ohhh that might be it. I will let ya know if this works. Thanks


#9

Ok, so here is the adjusted file (Just so ya know I’m not done with the print statements):

#Element Symbol and Atomic Weight:
elements = {
"none": {"name": "None", "atomic_weight": 0},
"H":  {"name": "Hydrogen", "atomic_weight": 1.00784},
"He": {"name": "Helium", "atomic_weight": 4.002602},
"Li": {"name": "Lithium", "atomic_weight": 6.938},
"Be": {"name": "Beryllium", "atomic_weight": 9.0121831},
"C":  {"name": "Carbon", "atomic_weight": 12.0096},
"O":  {"name": "Oxygen", "atomic_weight": 15.99903},
"N":  {"name": "Nitrogen", "atomic_weight": 14.00643},
"F":  {"name": "Fluorine", "atomic_weight": 18.99840316},
"Ne": {"name": "Neon", "atomic_weight": 20.1797},
"Na": {"name": "Sodium", "atomic_weight": 22.98976928},
"Mg": {"name": "Magnesium", "atomic_weight": 24.304},
"Al": {"name": "Aluminium", "atomic_weight": 26.9815385},
"Si": {"name": "Silicon", "atomic_weight": 28.084},
"P":  {"name": "Phosphorus", "atomic_weight": 30.973762},
"S":  {"name": "Sulfur", "atomic_weight": 32.059},
"Cl": {"name": "Chlorine", "atomic_weight": 35.446},
"Ar": {"name": "Argon", "atomic_weight": 39.948},
"K":  {"name": "Potassium", "atomic_weight": 39.0983},
"Ca": {"name": "Calcium", "atomic_weight": 40.078},
"Sc": {"name": "Scandium", "atomic_weight": 44.955908},
"Ti": {"name": "Titanium", "atomic_weight": 47.867},
"V":  {"name": "Vanadium", "atomic_weight": 50.9415},
"Cr": {"name": "Chromium", "atomic_weight": 51.9961},
"Mn": {"name": "Manganese", "atomic_weight": 54.938044},
"Fe": {"name": "Iron", "atomic_weight": 55.845},
"Co": {"name": "Cobalt", "atomic_weight": 58.933194},
"Ni": {"name": "Nickel", "atomic_weight": 58.6934},
"Cu": {"name": "Copper", "atomic_weight": 63.546},
"Zn": {"name": "Zinc", "atomic_weight": 65.38},
"Ga": {"name": "Gallium", "atomic_weight": 69.723},
"Ge": {"name": "Germanium", "atomic_weight": 72.63},
"As": {"name": "Arsenic", "atomic_weight": 74.921595},
"Se": {"name": "Selenium", "atomic_weight": 78.971},
"Br": {"name": "Bromine", "atomic_weight": 79.901},
"Kr": {"name": "Krypton", "atomic_weight": 83.798},
"Rb": {"name": "Rubidium", "atomic_weight": 85.4678},
"Sr": {"name": "Strontium", "atomic_weight": 87.62},
"Y":  {"name": "Yttrium", "atomic_weight": 88.90594},
"Zr": {"name": "Zirconium", "atomic_weight": 91.224},
"Nb": {"name": "Niobium", "atomic_weight": 92.90637},
"Mo": {"name": "Molybdenum", "atomic_weight": 95.95},
"Tc": {"name": "Technetium", "atomic_weight": 97},
"Ru": {"name": "Ruthenium", "atomic_weight": 101.07},
"Rh": {"name": "Rhodium", "atomic_weight": 102.9055},
"Pd": {"name": "Palladium", "atomic_weight": 106.42},
"Ag": {"name": "Silver", "atomic_weight": 107.8682},
"Cd": {"name": "Cadmium", "atomic_weight": 107.8682},
"In": {"name": "Indium", "atomic_weight": 114.818},
"Sn": {"name": "Tin", "atomic_weight": 118.71},
"Sb": {"name": "Antimony", "atomic_weight": 121.76},
"Te": {"name": "Tellurium", "atomic_weight": 127.6},
"I":  {"name": "Iodine", "atomic_weight": 126.90447},
"Xe": {"name": "Xenon", "atomic_weight": 131.293},
"Cs": {"name": "Caesium", "atomic_weight": 132.905452},
"Ba": {"name": "Barium", "atomic_weight": 137.327},
"La": {"name": "Lanthanum", "atomic_weight": 138.90547},
"Ce": {"name": "Cerium", "atomic_weight": 140.116},
"Pr": {"name": "Praseodymium", "atomic_weight": 140.90766},
"Nd": {"name": "Neodymium", "atomic_weight": 144.242},
"Pm": {"name": "Promethium", "atomic_weight": 145},
"Sm": {"name": "Samarium", "atomic_weight": 150.36},
"Eu": {"name": "Europium", "atomic_weight": 151.964},
"Gd": {"name": "Gadolinium", "atomic_weight": 157.25},
"Tb": {"name": "Terbium", "atomic_weight": 158.92535},
"Dy": {"name": "Dysprosium", "atomic_weight": 162.5},
"Ho": {"name": "Holmium", "atomic_weight": 164.93033},
"Er": {"name": "Erbium", "atomic_weight": 167.259},
"Tm": {"name": "Thulium", "atomic_weight": 168.93422},
"Yb": {"name": "Ytterbium", "atomic_weight": 173.045},
"Lu": {"name": "Lutetium", "atomic_weight": 174.9668},
"Hf": {"name": "Hafnium", "atomic_weight": 178.49},
"Ta": {"name": "Tantalum", "atomic_weight": 180.94788},
"W":  {"name": "Tungsten", "atomic_weigh": 183.84},
"Re": {"name": "Rhenium", "atomic_weight": 186.207},
"Os": {"name": "Osmium", "atomic_weight": 190.23},
"Ir": {"name": "Iridium", "atomic_weight": 192.217},
"Pt": {"name": "Platinum", "atomic_weight": 195.084},
"Au": {"name": "Gold", "atomic_weight": 196.966569},
"Hg": {"name": "Mercury", "atomic_weight": 200.592},
"Tl": {"name": "Thalium", "atomic_weight": 204.382},
"Pb": {"name": "Lead", "atomic_weight": 207.2},
"Bi": {"name": "Bismuth", "atomic_weight": 208.9804},
"Po": {"name": "Polonium", "atomic_weight": 209},
"At": {"name": "Astatine", "atomic_weight": 210},
"Rn": {"name": "Radon", "atomic_weight": 222},
"Fr": {"name": "Francium", "atomic_weight": 223},
"Ra": {"name": "Radium", "atomic_weight": 226},
"Ac": {"name": "Actinium", "atomic_weight": 227},
"Th": {"name": "Thorium", "atomic_weight": 232.0377},
"Pa": {"name": "Protactinium", "atomic_weight": 231.03588},
"U":  {"name": "Uranium", "atomic_weight": 238.02891},
"Np": {"name": "Neptunium", "atomic_weight": 237},
"Pu": {"name": "Plutonium", "atomic_weight": 244},
"Am": {"name": "Americium", "atomic_weight": 243},
"Cm": {"name": "Curium", "atomic_weight": 247},
"Bk": {"name": "Berkelium", "atomic_weight": 247},
"Cf": {"name": "Californium", "atomic_weight": 251},
"Es": {"name": "Einsteinium", "atomic_weight": 252},
"Fm": {"name": "Fermium", "atomic_weight_": 257},
"Md": {"name": "Mendelevium", "atomic_weight": 258},
"No": {"name": "Nobelium", "atomic_weight": 259},
"Lr": {"name": "Lawrencium", "atomic_weight": 262},
"Rf": {"name": "Rutherfordium", "atomic_weight": 263},
"Db": {"name": "Dubnium", "atomic_weight": 268},
"Sg": {"name": "Seaborgium", "atomic_weight": 271},
"Bh": {"name": "Bohrium", "atomic_weight": 270},
"Hs": {"name": "Hassium", "atomic_weight": 270},
"Mt": {"name": "Meitnerium", "atomic_weight": 278},
"Ds": {"name": "Darmstadtium", "atomic_weight": 281},
"Rg": {"name": "Roentgenium", "atomic_weight": 281},
"Cn": {"name": "Copernicium", "atomic_weight": 285},
"Ni": {"name": "Nihonium", "atomic_weight": 286},
"Fl": {"name": "Flerovium", "atomic_weight": 289},
"Mc": {"name": "Moscovium", "atomic_weight": 289},
"Lv": {"name": "Livermorium", "atomic_weight": 293},
"Ts": {"name": "Tennessine", "atomic_weight": 294},
"Og": {"name": "Organesson", "atomic_weight": 294},
}


def composition_of_compounds_calculator(element1,element2,element3):
    if len(element1) == 2 and len(element2 == 2 and len(element3) == 2):
        #Molar Mass Calculations:
        molar_info_element1 = elements[element1[0]].atomic_weight * element1[1]
        molar_info_element2 = elements[element2[0]].atomic_weight * element2[1]
        molar_info_element3 = elements[element3[0]].atomic_weight * element3[1]
        molar_mass = molar_info_element1 + molar_info_element2 + molar_info_element3
        #Percent Composition Calculations:
        percent_composition_element1 = (molar_info_element1 / molar_mass) * 100
        percent_composition_element2 = (molar_info_element1 / molar_mass) * 100
        percent_composition_element3 = (molar_info_element1 / molar_mass) * 100
        #Print_Statements:
        print("")
        print("Molar Mass of compound:")
        print(molar_mass)
    return molar_info_element1

#10

Now that you have a nice, neat dictionary, the length of the inputs are of no importance. You have a lookup table. The input is in the table, or it is not. If any inputs do not appear in the table, abort and do not go further.


#11

We aren’t checking if it’s an element, we are checking if it is a single compound or a multi compound.

Here is an example of a single compound:
composition_of_compounds([Na,3],[P,1],[O,4])
composition_of_compounds([Ag,1,Na,2,H,4,2],[F,1],[O,0])
Here is an example of a multi compound:


#12

You’ve lost me. Above you have three length checks that I’ve suggested are no longer warranted. What has that to do with this reply?


#13

Why do you think that I should get rid of the length checks?


#14

Because keys should not be under that restraint.


#15

Ok, I see. Thanks for the help as always


#16

Any time. Looking forward to see the next iteration. Might even give it a go myself if you tag me in midstream. Cheers!


#17

Something else I should make you aware of… In an earlier statement I referred to an instance object

There is no class, save the type. That came from the declaration itself.