例如,水的分子量(H20)为: 2(1.00794) + 15.9994 = 18代码:
formula = int(input("Enter the number of elements: "))
for i in range(formula):
element = input("enter your element: ")
molCount = float(input("enter the molecule count: "))
print(molCount)
atomWeight = float(input("enter the atomic weight: "))
print(atomWeight)
total = molCount*atomWeight
print(total)
total = total + total
print(total)需要有关将多个元素添加到一起的帮助...
发布于 2017-10-04 08:18:37
线路上有一个错误:total = molCount*atomWeight
您可能指的是total += molCount*atomWeight。
发布于 2017-10-04 08:34:05
首先,在for循环之前定义total。其次,我认为你的方程式需要
total += (molCount * atomWeight)而不是原来的。编辑代码:
formula = int(input("Enter the number of elements: "))
total = 0
for i in range(formula):
molCount = int(input("Enter the molecule count: "))
print("Molecule count: " + str(molCount))
atomWeight = float(input("Enter the atomic weight: "))
print("Atomic Weight: " + str(atomWeight))
total += (molCount * atomWeight)
print(total)https://stackoverflow.com/questions/46555235
复制相似问题