我很难把摩尔质量换算成克。这个程序显示化合物A的摩尔质量为x克(用户输入)。 如果我有10克二氧化硅,那么硅和氧分别是多少克?在
我该如何对用户输入的10g二氧化硅进行编码,并将其转换为需要多少g的Si和g的O?在
10gSiO2/M二氧化硅*MSi=gSi
2*(10g SiO2/M SiO2)*MO=gO(乘以2,因为O2)
M=摩尔质量
g=克
m=摩尔
import Periodic_Table_new as pt
print("Enter info for Compound A\n")
compoundA = pt.getInput()
gramTotal = float(input("Please enter total grams of Compound A: "))
moles = pt.convertToMoles(gramTotal,compoundA)
moleMass = moles/compoundA
print('\n')
print("The Molecular Mass of compound A at " + str(gramTotal) + "grams is : " + str(moleMass) + "\n\n")
周期表_新建.py在
^{pr2}$
根据我对你期望的结果的理解,我想出了一个不同的方法。这个版本将使用原子量字典,regex解析复合字符串,并使用循环来计算。它非常灵活,但是它需要你建立原子质量的字典,这不难,只是有点乏味。它对评论中提到的输入有一些要求
输出示例:
^{pr2}$编辑:
为了适应输入的十进制质量,我们可以将}等值。它仍然不需要kg或mg,但是如果您查找内置的}
amount_pat
中的regex改为r'(\d*.\d+)g'
。现在它将接受.003g
或{re
包,您可以了解regex是如何工作的,并从中进行更改。regex101对于regex也是一个很好的资源,因为它允许尝试和错误的方法来学习。我还在mass
赋值语句中将int
改为{不过,我改变了元素质量环,以适应mg和kg。如果您希望进一步扩展,只需采用我在这里开始的模式并进一步扩展:
新示例输出:
你需要元素在最后,那是缺失的部分。在
如果没有输入魔法,以下是10g SiO2的示例:
相关问题 更多 >
编程相关推荐