Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- # elements and their mass
- elements = {
- 'C' : 12,
- 'H' : 1,
- 'N' : 14,
- 'O' : 16,
- 'S' : 32,
- 'Cl' : 35.5,
- 'P' : 31
- }
- def molar_mass(formula):
- """ Returns a molar mass of compound.
- Uses single argument - formula of compound
- """
- s = 0
- for i in elements.keys():
- f = re.findall(i+'[0-9]*',formula)
- if f:
- tmp = f[0]
- num = re.findall('[0-9]+',tmp)
- if num == []:
- s += elements[tmp]
- else:
- s += elements[i] * int(num[0])
- return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement