我是个编程新手,在我的计算物理课上,我被分配了这个任务:class assignment
这是我为它编写的代码:
A = int(input("please enter the atomic mass"))
Z = int(input("please enter the atomic number"))
def semi_empirical_mass_formula(A,Z):
a1 = 15.8
a2 = 18.3
a3 = 0.714
a4 = 23.2
afive = [-12.0,12.0,0.0]
if A%2 == 0 and Z%2 != 0:
a5=afive[0]
if A%2 and Z%2 == 0:
a5=afive[1]
if A%2 != 0:
a5=afive[2]
return (a1*A-a2*A**(2/3))-((a3*Z**2)/A**(1/3))-((a4*(A-2*Z)**2)/A)+((-12.0)/A**(1/2))
print (semi_empirical_mass_formula(A,Z))正如你可能看到的,它不工作,我不知道为什么它给我的答案是324.724。答案应该是~500
我对学习很感兴趣,我的教授鼓励我们向像这样的程序员社区提问。请包括如何修复它的说明。最后这一部分会让我受益匪浅,因为我在编码方面非常缺乏经验。
编辑:我还使用了"//“作为除法,当我这样做时,它给我的答案是326.1
发布于 2021-03-01 07:13:49
4.5年后,你可能仍然不需要这个,但是对于其他有这个问题并寻求帮助的人来说,这是我在Mark Newman的Python Programming for Physicists (有时也称为计算物理)中回答练习2.10A的答案。
A = float(input("Please enter the atomic mass number "))
Z = float(input("Please enter the atomic number "))
a1 = 15.8
a2 = 18.3
a3 = .714
a4 = 23.2
if A % 2 == 1:
a5 = 0
elif A % 2 == 0 and Z % 2 == 0 :
a5 = 12.0
else:
a5 = -12.0
B = a1*A - a2*(A**(2/3)) - a3*(Z**2)/(A**(1/3)) - a4*((A-2*Z)**2)/A + a5/(A**(1/2))
print("The nuclear binding energy is: ", round(B, 2))https://stackoverflow.com/questions/39563493
复制相似问题