错误表示“如果e\u level==1:”没有名为“e\u level”的变量

2024-05-04 10:09:13 发布

您现在位置:Python中文网/ 问答频道 /正文

一直说我测试的时候变量没有定义;我使用了各种不同的方法来尝试解决这个问题,比如把变量移到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)

Tags: the方法in程序目的numberforinput
1条回答
网友
1楼 · 发布于 2024-05-04 10:09:13

从这一行开始,您的输入将是str

atomic_number = raw_input("Input elements atomic number: ")

你需要把它转换成int

atomic_number = int(raw_input("Input elements atomic number: "))

如果将strint进行比较,str总是更大

>>> '99' < 5
False
>>> 99 < '5'
True

相关问题 更多 >