Skip to content

Instantly share code, notes, and snippets.

@appleyface
Created April 23, 2017 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appleyface/8f033479b167c68dbc29b40ea2d392d8 to your computer and use it in GitHub Desktop.
Save appleyface/8f033479b167c68dbc29b40ea2d392d8 to your computer and use it in GitHub Desktop.
molar mass calculator
import csv
import urllib
import os.path
if not os.path.isfile('periodictabledump.csv'):
print "file doesn't exist yet...retrieved from Internet"
url = "http://akiscode.com/project_files/pt/periodictabledump.csv"
urllib.urlretrieve(url, "periodictabledump.csv")
with open('periodictabledump.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter = ',')
molar_masses=[]
elements=[]
for row in readCSV:
molar_mass = row[1]
element = row[3]
molar_masses.append(molar_mass)
elements.append(element)
# print(molar_masses)
# print(elements)
print "write the abbreviation of the element name and capitalize the first letter."
whatElement = raw_input("What element do you wish to know the molar mass of?")
elemdex = elements.index(whatElement)
theMass = float(molar_masses[elemdex])
newTotal = float(molar_masses[elemdex])
print 'The molar mass of',whatElement, 'is:',theMass
print "What do you wish to do now?"
print """
[A] I want to add this given molar mass to the molar mass of another element
[B] I want to multiply this given molar mass by some number
[C] I want to find the molar mass of a new element
[D] I wish to exit
"""
choice = ""
while choice != "D" or choice == "A" or choice == "B" or choice == "C":
choice = raw_input("Enter the letter of your choice:")
if choice == "A":
whatElementadd = raw_input("Which element's molar mass do you want to add?")
elemdex = elements.index(whatElementadd)
theMassadd = ((float(molar_masses[elemdex])) + newTotal)
newTotal = ((float(molar_masses[elemdex])) + newTotal)
print 'The total molar mass is:', newTotal
elif choice == "B":
multiplied = int(raw_input("What number do you want to multiply the element's molar mass by?"))
multipliedelementmass = multiplied * newTotal
newTotal = multipliedelementmass = multiplied * newTotal
print 'The total molar mass is:', multipliedelementmass
elif choice == "C":
newElement = raw_input("What new element do you wish to know the molar mass of?")
elemdex = elements.index(newElement)
theMassnewMass = float(molar_masses[elemdex])
print 'The molar mass of', newElement, 'is:', theMassnewMass
elif choice == "D":
input("Press 'Enter' to confirm that you wish to exit.")
else:
print("You inputted an invalid command.")
print "What do you wish to do now?"
print """
[A] I want to add the molar mass of this element to the molar mass of another element
[B] I want to multiply the molar mass of this element by some number
[C] I want to find the molar mass of a new element
[D] I wish to exit
"""
input("Enter to exit")
# csv file from http://akiscode.com/getarticle.php?id=10
# with open('periodictabledump.csv') as csvfile:
# readCSV = csv.reader(csvfile, delimiter = ',')
# for row in readCSV:
# print row[0] + " - " + row[2]
# print "\tMolar Mass: %s" % row[1]
# print "\tSymbol: %s" % row[3]
# print "\n"
# # print row[3] + ' = ' + row[1] #prints element name equal to its molar mass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment