Код: Выделить всё
mass = {"H":1, "O":16, "C":12, "N":14}
def moleculeMass(moleculeName):
p = 0
total = 0
for i in moleculeName:
if i.isalpha():
p = i
total += mass[i]
else:
total = total - mass[p] + (mass[p] * int(i))
return total
compoundName = input("input molecule name: ")
print(moleculeMass(compoundName))
Подробнее здесь: https://stackoverflow.com/questions/704 ... ar-weights