Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. tabela = {'H':1, 'S':32, 'O':16, 'C':12, 'Ca':40, 'Na':23, 'P':31}
  2.  
  3. def massa(tabela, string):
  4. molecula = string.split()
  5. massa_m = 0
  6. i = 0
  7. while i < len(molecula):
  8. if i == (len(molecula) - 1) and molecula[i].isalpha():
  9. for k in tabela:
  10. if molecula[i] == k:
  11. massa_m += tabela[k]
  12. break
  13. elif molecula[i + 1].isdigit():
  14. for k in tabela:
  15. if molecula[i] == k:
  16. massa_m += int(molecula[i + 1]) * tabela[k]
  17. break
  18. i += 2
  19. else :
  20. for k in tabela:
  21. if molecula[i] == k:
  22. massa_m += tabela[k]
  23. break
  24. i += 1
  25. return massa_m
  26.  
  27. string = 'C O 2'
  28. print massa(tabela, string)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement