首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python|元素的重复添加

Python|元素的重复添加
EN

Stack Overflow用户
提问于 2017-10-04 08:14:37
回答 2查看 43关注 0票数 0

例如,水的分子量(H20)为: 2(1.00794) + 15.9994 = 18代码:

代码语言:javascript
复制
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)

需要有关将多个元素添加到一起的帮助...

EN

回答 2

Stack Overflow用户

发布于 2017-10-04 08:18:37

线路上有一个错误:total = molCount*atomWeight

您可能指的是total += molCount*atomWeight

票数 0
EN

Stack Overflow用户

发布于 2017-10-04 08:34:05

首先,在for循环之前定义total。其次,我认为你的方程式需要

代码语言:javascript
复制
total += (molCount * atomWeight)

而不是原来的。编辑代码:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46555235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档