我在尝试将摩尔质量转换回克时遇到了一些困难。该程序以x克(用户输入)吐出化合物A的摩尔质量。 如果我有10克的SiO2,那么Si和O2的克数(相应地)是多少?
我如何编写用户输入的10g二氧化硅并将其转换为需要多少的Si和g的O?
10gSiO 2 / M SiO 2 * MSi = gSi
2 *(10g SiO2 / M SiO2)* MO = gO(因为O2乘以2)
M =摩尔质量
g =克
m = mole
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")
Periodic_Table_new.py
def getInput():
compoundName = ""
ix = True
total = 0
while ix:
elementName = str(input("Please Enter Element: "))
amountNum = int(input("Please Enter Amount of Element: "))
elementWeight = float(input("Please enter Element Weight: "))
elementWeight = amountNum * elementWeight
total = total + elementWeight
compoundName = compoundName + elementName + str(amountNum)
print(compoundName)
userInput = input("Would you like to repeate? \n Y/N? ")
if userInput == 'N' or userInput == 'n':
ix = False
print(compoundName)
print(total)
return total
def convertToMoles(gOc,c):
moles = gOc/c
return moles
def molesToGrams():
p = moles * n * m
答案 0 :(得分:0)
你需要最后的元素,那就是缺少的部分。
没有输入法术,这里是10g SiO2的例子:
target=10
elem=[['Si',1,28.09],['O',2,16]]
total=0
for e in elem:
total=total+e[1]*e[2]
print('molar mass of compound:',total)
moles=target/total
print('moles:',moles)
for e in elem:
print(e[0],moles*e[1],'mol',moles*e[1]*e[2],'g')
答案 1 :(得分:0)
根据我对你理想结果的理解,我提出了一些不同的方法。这个版本将使用原子质量字典,正则表达式解析复合字符串,以及循环来进行数学运算。它非常灵活,但它需要你建立原子质量的字典,这应该不难,只是有点单调乏味。它对评论中提到的输入有一些要求
import re
atomic_masses = {'Si': 28.0855, 'O': 15.999}
compound = input('Please enter a compound and amount: ')
amount_pat = re.compile(r'(\d+)g') # compound mass must end in the letter g
element_pat = re.compile(r'([A-Z][a-z]?\d*)') # elemental symbols must be properly capitalized (Si, not si)
sym_pat = re.compile(r'[A-Z][a-z]?') # elemental symbols must be properly capitalized (Si, not si)
qty_pat = re.compile(r'\d+')
mass = int(amount_pat.search(compound)[1])
elements = []
# finds each element in the compound and makes a list of (element, parts) tuples
for element in element_pat.finditer(compound):
element = element[0]
symbol = sym_pat.search(element)[0]
if any(c.isdigit() for c in element):
qty = int(qty_pat.search(element)[0])
else:
qty = 1
elements.append((symbol, qty))
# Calculates and prints total Molecular Mass for the compund
molecular_mass = sum(el[1] * atomic_masses[el[0]] for el in elements)
print(f'\nTotal Molecular Mass: {molecular_mass}\n')
# Calculates and prints the mass for each element
for tup in elements:
unit_mass = (mass / molecular_mass) * atomic_masses[tup[0]] * tup[1]
print(f'{tup[0]}: {unit_mass:.4f}g')
示例输出:
Please enter a compound and amount: 10g SiO2
Total Molecular Mass: 60.0835
Si: 4.6744g
O: 5.3256g
为了适应输入的十进制质量,我们可以将amount_pat
内的正则表达式更改为r'(\d*.\d+)g'
。现在它将接受.003g
或0.003g
等值。它仍然不需要kg或mg,但是如果你查找内置的re
包,你就可以了解regex如何工作并从那里改变它。 regex101也是正则表达式的一个很好的资源,因为它允许使用试错法进行学习。我还在int
转让声明
float
更改为mass
然而,我确实改变了元素质量循环以调整mg和kg。如果你想进一步扩展它,只需采取我在这里开始的模式并进一步扩展它:
for tup in elements:
unit_mass = (mass / molecular_mass) * atomic_masses[tup[0]] * tup[1]
if unit_mass > 1000:
unit_mass /= 1000
unit = 'kg'
elif unit_mass < .01:
unit_mass *= 1000
unit = 'mg'
else:
unit = 'g'
print(f'{tup[0]}: {unit_mass:.4f}{unit}')
新示例输出:
Please enter a compound and amount: .003g H2O
Total Molecular Mass: 18.01488
H: 0.3357mg
O: 2.6643mg