Advertisement
atm-irbis

Molar mass

May 4th, 2013
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.58 KB | None | 0 0
  1. import re
  2. # elements and their mass
  3. elements = {
  4.   'C' : 12,
  5.   'H' : 1,
  6.   'N' : 14,
  7.   'O' : 16,
  8.   'S' : 32,
  9.   'Cl' : 35.5,
  10.   'P' : 31
  11. }
  12.  
  13. def molar_mass(formula):
  14.     """ Returns a molar mass of compound.
  15.        Uses single argument - formula of compound
  16.    """
  17.     s = 0
  18.     for i in elements.keys():
  19.         f = re.findall(i+'[0-9]*',formula)
  20.         if f:
  21.             tmp = f[0]
  22.             num = re.findall('[0-9]+',tmp)
  23.             if num == []:
  24.                 s += elements[tmp]
  25.             else:
  26.                 s += elements[i] * int(num[0])
  27.     return s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement