一直说我测试的时候变量没有定义;我使用了各种不同的方法来尝试解决这个问题,比如把变量移到if之外,这样做是可行的,但是我必须对每个变量都这样做。另一个问题是,我的程序不能正常工作,因此不能达到它的目的。我已经做了各种不同的编程语言,但我是非常新的python和我会非常感谢任何和所有的帮助
#collects user input
atomic_number = raw_input("Input elements atomic number: ")
#finds level and electrons in valence level
#good if e_level <= 4
if atomic_number <= 2:
e_level = 1
e_in_v = atomic_number
if atomic_number <= 8:
e_level = 2
e_in_v = atomic_number - 2
if atomic_number <= 18:
e_level = 3
e_in_v = atomic_number - 10
if atomic_number <= 32:
e_level = 4
e_in_v = atomic_number - 18
#construct the electron configuration
if e_level == 1:
e_c = "1s^" + e_in_v
if e_level == 2:
if e_in_v <= 2:
e_c = "1s^2 2s^" + e_in_v
if e_in_v >= 3:
e_for_e_c = e_in_v - 2
e_c = "1s^2 2s^2 2p^" + e_for_e_c
if e_level == 3:
if e_in_v <= 2:
e_c = "1s^2 2s^2 2p^6 3s^" + e_in_v
if e_in_v <= 6:
e_for_e_c = e_in_v - 2
e_c = "1s^2 2s^2 2p^6 3s^2 3p^" + e_for_e_c
if e_in_v >= 7:
e_for_e_c = e_in_v - 8
e_c = "1s^2 2s^2 2p^6 3s^2 3p^6 3d^" + e_for_e_c
if e_level == 4:
if e_in_v <= 2:
e_c = "1s^2 2s^2 2p^6 3s^2 3p^6 3d^10 4s^" + e_in_v
if e_in_v <= 6:
e_for_e_c = e_in_v - 2
e_c = "1s^2 2s^2 2p^6 3s^2 3p^6 3d^10 4s^2 4p^" + e_for_e_c
if e_in_v <= 10:
e_for_e_c = e_in_v - 8
e_c = "1s^2 2s^2 2p^6 3s^2 3p^6 3d^10 4s^2 4p^6 4d^" + e_for_e_c
if e_in_v >= 11:
e_for_e_c = e_in_v - 18
e_c = "1s^2 2s^2 2p^6 3s^2 3p^6 3d^10 4s^2 4p^6 4d^10 4f^" + e_for_e_c
#prints the electron configuration
print(e_c)
从这一行开始,您的输入将是
str
你需要把它转换成
int
如果将
str
与int
进行比较,str
总是更大相关问题 更多 >
编程相关推荐