Python Forum
Thread Rating:
  • 2 Vote(s) - 1.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AP Chem
#1
Hello, I am in AP Chemistry at my high school and I was trying to find ways to make it a little bit easier. Obviously the easiest way to make life simple in chemistry is to determine the mass of 1 or more elements. So I started off with a dictionary and I wrote the chemicals and then their mass. I was wondering if there is a more simple way to add them together instead of having to write each individual possibility? Thanks
Reply
#2
Can you show an example?
Reply
#3
Mass_Of_Elements = {
"H": "1.008 g",
"He": "4.003 g",
"Li": "6.941 g",
"Be": "9.012 g",
"B": "10.811 g",
So this is essentially what I have. So if I want to add Helium and Boron for example. Would I need to make a string for just the every single possibility or is there a way I can just let python know that I am adding Boron and Helium. Also these are only the first 5 elements. I am going to use all of them. Or attempt to at least
Reply
#4
Please use python and output tags when posting code and results. Here are instructions on how to do it.

I'm no chemist, but if you enter the values as numbers, you can add them together:

>>> Mass_Of_Elements = {'H': 1.008, 'He': 4.003, 'Li': 6.941, 'Be': 9.012, 'B': 10.811}
>>> Mass_Of_Elements['He'] + Mass_Of_Elements['B']
14.814
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Ok I apologize for not using the tag. I was unsure what it meant. But from now on I will use it. So last question. How would I get the user to plug in their own elements and have it come up on the console?
Reply
#6
The input function. It takes a string (the question to ask) as a parameter, asks the user the question, and returns the user's response as a string.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
print("Please use --- If you do not wish to add another mass")
E1 = input("What is your first element?")
E2 = input("What is your second element?")
E3 = input("What is your third element?")
E4 = input("What is your fourth element?")
E5 = input("What is your fifth element?")
Mass_Of_Elements = {}
answer = Mass_Of_Elements[float(E1)] + Mass_Of_Elements[float(E2)] + Mass_Of_Elements[float(E3)] + Mass_Of_Elements[float(E4)] + (Mass_Of_Elements[float(E5)])
print(answer)
Hope I copied this right. Ok so where is my error here?? I tried to float all of the numbers that were in the dictionary and I just get an error saying "could not convert string to float 'He'"
Reply
#8
No, you need two pieces for each element of a dictionary: a key and a value. Read metulbur's dictionary tutorial and try again.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
print("Please use --- If you do not wish to add another mass")
E1 = input("What is your first element?")
E2 = input("What is your second element?")
E3 = input("What is your third element?")
E4 = input("What is your fourth element?")
E5 = input("What is your fifth element?")
Mass_Of_Elements = {
    "H": "1.008",
    "He": "4.003",
    "Li": "6.941",
    "Be": "9.012",
    "B": "10.811",
    "C": "12.011",
    "N": "14.007",
    "O": "15.999",
    "F": "18.998",
    "Ne": "20.180",
    "Na": "22.990",
    "Mg": "24.305",
    "Al": "26.982",
    "Si": "28.086",
    "P": "30.974",
    "S": "32.066",
    "Cl": "35.453",
    "Ar": "39.948",
    "K": "39.098",
    "Ca": "40.078",
    "Sc": "44.956",
    "Ti": "47.867",
    "V": "50.942",
    "Cr": "51.996",
    "Mn": "54.938",
    "Fe": "55.845",
    "Co": "58.933",
    "Ni": "58.693",
    "Cu": "63.546",
    "Zn": "65.38",
    "Ga": "69.723",
    "Ge": "72.631",
    "As": "74.922",
    "Se": "78.971",
    "Br": "79.904",
    "Kr": "84.798",
    "Rb": "84.468",
    "Sr": "87.62",
    "Y": "88.906",
    "Zr": "91.224",
    "Nb": "92.906",
    "Mo": "95.95",
    "Tc": "98.907",
    "Ru": "101.07",
    "Rh": "102.906",
    "Pd": "106.42",
    "Ag": "107.868",
    "Cd": "112.411",
    "In": "114.818",
    "Sn": "118.711",
    "Sb": "121.760",
    "Te": "127.6",
    "I": "126.904",
    "Xe": "131.294",
    "Cs": "132.905",
    "Ba": "137.328",
    "La": "138.905",
    "Ce": "140.116",
    "Pr": "140.908",
    "Nd": "144.243",
    "Pm": "144.913",
    "Sm": "150.36",
    "Eu": "151.964",
    "Gd": "157.25",
    "Tb": "158.925",
    "Dy": "162.500",
    "Ho": "164.930",
    "Er": "167.259",
    "Tm": "168.934",
    "Yb": "173.055",
    "Lu": "174.967",
    "Hf": "178.49",
    "Ta": "180.948",
    "W": "183.84",
    "Re": "186.207",
    "Os": "190.23",
    "Ir": "192.217",
    "Pt": "195.085",
    "Au": "196.967",
    "Hg": "200.592",
    "Tl": "204.383",
    "Pb": "207.2",
    "Bi": "208.980",
    "Po": "208.982",
    "At": "209.987",
    "Rn": "222.018",
    "Fr": "223.020",
    "Ra": "226.025",
    "Ac": "227.028",
    "Th": "232.038",
    "Pa": "231.036",
    "U": "238.029",
    "Np": "237.048",
    "Pu": "244.064",
    "Am": "243.061",
    "Cm": "247.070",
    "Bk": "247.070",
    "Cf": "251.080",
    "Es": "254",
    "Fm": "257.095",
    "Md": "258.1",
    "No": "259.101",
    "Lr": "262",
    "Rf": "261",
    "Db": "262",
    "Sg": "266",
    "Bh": "264",
    "Hs": "269",
    "Mt": "268",
    "Ds": "269",
    "Rg": "272",
    "Cn": "277",
    "---": "0.0000",

}
answer = Mass_Of_Elements[float(E1)] + Mass_Of_Elements[float(E2)] + Mass_Of_Elements[float(E3)] + Mass_Of_Elements[float(E4)] + Mass_Of_Elements[float(E5)]
print(answer)
For sake of saving time with you reading I did have it correct. Or I think I do at least. do you see an error?
Reply
#10
No, you got it a little backwards. Look at my example again:

>>> Mass_Of_Elements = {'H': 1.008, 'He': 4.003, 'Li': 6.941, 'Be': 9.012, 'B': 10.811}
>>> Mass_Of_Elements['He'] + Mass_Of_Elements['B']
14.814
First, note in the first line that the atomic weights are stored as numbers, not as strings (no quotes). Then in the second line, not that the keys are the atomic symbols.

In your code you have Mass_Of_Elements[float(E1)]. But if the first element they enter is 'He', this will try to do float('He'), which will cause an error. You could make this work by instead doing float(Mass_Of_Elements[E1]). Then if the user enteres 'He', Mass_Of_Elements[E1] returns '4.003' (a string). That string goes to float('4.003') which returns the number 4.003. But if you just store them as numbers in the first place, you don't have to convert them to numbers, and you don't have to worry about where the float call goes.

Always try to keep things simple when programming. The more complicated you make things, the more likely they will have errors, and the harder they will be to maintain.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020