我正在尝试开发一个简单的 Python 程序来计算化合物的分子式质量。我面临两个问题:
'b' 显然存在语法错误,但我不知道它是什么。这是我到目前为止所做的:
def FormulaMass():
H = 1
He = 4
Li = 7
Be = 9
B = 11
C = 12
N = 14
O = 16
F = 19
Ne = 20
Na = 23
Mg = 24
Al = 27
Si = 28
P = 31
S = 32
Cl = 35.5
Ar = 40
K = 39
Ca = 40
Sc = 45
Ti = 48
V = 51
Cr = 52
Mn = 55
Fe = 56
Co = 59
Ni = 59
Cu = 63.5
Zn = 65
Ga = 70
Ge = 73
As = 75
Se = 79
Br = 80
Rb = 85.5
Sr = 88
Y = 89
Zr = 91
Nb = 93
Mo = 96
Tc = 98
Ru = 101
Rh = 103
Pd = 106.5
Ag = 108
Cd = 112.5
In = 115
Sn = 119
Sb = 122
Te = 128
I =127
Xe = 131
Cs = 133
Ba = 137
La = 139
Ce = 140
Pr = 141
Nd = 144
Pm = 145
Sm = 150
Eu = 152
Gd = 157
Tb = 159
Dy = 162.5
Ho = 165
Er = 167
Tm = 169
Yb = 173
Lu = 175
Hf = 178.5
Ta = 181
W = 184
Re = 186
Os = 190
Ir = 192
Pt = 195
Au = 197
Hg = 201
Tl = 204
Pb = 207
Bi = 209
Po = 209
At = 210
Rn = 222
Fr = 223
Ra = 226
Ac = 227
Th = 232
Pa = 231
U = 238
Np = 237
Pu = 244
Am = 243
Cm = 247
Bk = 247
Cf = 251
Es = 252
Fm = 257
Md = 258
No = 259
Rf = 261
Db = 262
Sg = 266
Bh = 264
Hs = 277
Mt = 268
Ds = 271
Rg = 272
Uub = 285
Uut = 284
Uuq = 289
Uup = 288
Uuh = 292
Uuo = 294
element = [H, He, Li, Be, B. C, N, O, F, Ne, Na, Mg, Al, Si, P, S, Cl, Ar, K, Ca, Sc, Ti, V, Cr, Mn, Fe, Co, Ni, Cu, Zn, Ga, Ge, As, Se, Br, Rb, Sr, Y, Zr, Nb, Mo, Tc, Ru, Rh, Pd, Ag, Cd, In, Sn, Sb, Te, I, Xe, Cs, Ba, La, Ce, Pr, Nd, Pm, Sm, Eu, Gd, Tb, Dy, Ho, Er, Tm, Yb, Lu, Hf, Ta, W, Re, Os, Ir, Pt, Au, Hg, Tl, Pb, Bi, Po, At, Rn, Fr, Ra, Ac, Th, Pa, U, Np, Pu, Am, Cm, Bk, Cf, Es, Fm, Md, No, Rf, Db, Sg, Bh, Hs, Mt, Ds, Rg, Uub, Uut, Uuq, Uup, Uuh, Uuo]
a = raw_input('Which' + str(element) + '?')
b = float(raw_input('How many moles?'))
c = str(raw_input('Is that all [Y/N]?'))
while c == 'N':
print
- 'a' 在运行代码时实际上并没有出现,它只是立即在 'b' 中识别出这个语法错误。
我试图用'a'做的是允许用户从列表'元素'中输入一个常数,以便质量(取决于可以计算的摩尔数)。现在我看到的一个潜在问题是,我不确定如何允许用户输入具有不同摩尔数的不同元素,而不会创建无穷无尽的常数(例如 a、b、c...)。
目的是在末尾添加 a*b 以找到质量,但有没有办法制作多个 a 和 b,所以理论上用户可以拥有 a*b + a1*b1 ...
PS很抱歉没有正确输入我的代码,我在每行后面放4个缩进需要太长时间:/