为什么输出不会根据用户的输入而改变?

时间:2017-12-08 22:31:31

标签: python if-statement

所以我正在做我的学校工作,我需要做的一个编程挑战是周期表挑战。我不认为编程会很难,但我遇到了一个我无法解决的问题。这不是一个语法错误,我已经花了几个小时在互联网上通过指南我是否正确使用if语句,但我无法识别错误。我的代码是:

lithium = ("""Name: Lithium
Atmomic mass: 6.94
Group: Alkali Metals""")

sodium = ("""Name: Sodium
Atomic mass: 22.989769 u ± 2 × 10^-8 u
Group: Alkali Metals""")

potassium = ("""Name: Potassium
Atmoic mass: 39.0983 u ± 0.0001 u
Group: Alkali Metals""")

rubidium = ("""Name: Rubidium
Atomic mass: 85.4678 u ± 0.0003 u
Group: Alkali Metals""")

cesium = ("""Name: Ceasium
Atomic mass: 132.90545 u ± 2 × 10^-7 u
Group: Alkali Metals""")

francium = ("""Name: Francium
Atomic mass: 223 u
Group: Alkali Metals""")

################################################################################################################################################################

helium = ("""Name: Helium
Atomic Mass: 4.002602 u ± 0.000002 u
Group: Noble Gasses""")

neon = ("""Name: Neon
Atomic Mass: 20.1797 u ± 0.0006 u
Group: Noble Gasses""")

argon = ("""Name: Argon
Atomic Mass: 39.948 u ± 0.001 u
Group: Noble Gasses""")

krypton = ("""Name: Krypton
Atomic Mass: 83.798 u
Group: Noble Gasses""")

xenon = ("""Name: Xenon
Atomic Mass: 131.293 u ± 0.006 u
Group: Noble Gasses""")

radon = ("""Name: Radon
Atomic Mass: 222 u
Group: Noble Gasses""")

def noble():
    print(helium)
    print(" ")
    print(neon)
    print(" ")
    print(argon)
    print(" ")
    print(krypton)
    print(" ")
    print(xenon)
    print(" ")
    print(radon)

def alkali():
    print(lithium)
    print(" ") 
    print(sodium)
    print(" ") 
    print(potassium)
    print(" ") 
    print(rubidium)
    print(" ") 
    print(cesium)
    print(" ") 
    print(francium)
print ("""Please choose the element you are looking for from the list below
Lithium
Sodium
Potassium
Rubidium
Caesium
Francium
Helium
Neon
Argon
Krypton
Xenon
Radon

Alternatively you can type in the name of one of these groups to get the data for first 6 elements in the group

Noble Gasses
Alkali Metals""")

userchoice = str(input())


if userchoice == "Helium" or "helium":
    print(helium)
elif userchoice == "Neon" or "neon":
    print(neon)
elif userchoice == "Argon" or "argon":
    print(argon)
elif userchoice == "Krypton" or "krypton":
    print(krypton)
elif userchoice == "Xenon" or "xenon":
    print(xenon)
elif userchoice == "Radon" or "radon":
    print(radon)
elif userchoice == "Lithium" or "lithium":
    print(lithium)
elif userchoice == "Sodium" or "sodium":
    print(sodium)
elif userchoice == "Potassium" or "potassium" or "Potasium" or "potasium":
    print(potassium)
elif userchoice == "Rubidium" or "rubidium":
    print(rubidium)
elif userchoice == "Caesium" or "caesium" or "Cesium" or "cesium":
    print(cesium)
elif userchoice == "Francium" or "francium":
    print(francium)
elif userchoice == "Noble Gasses" or "noble gasses" or "noble" or "gasses":
    print(noble())
elif userchoice == "Alkali Metals" or "alkali metals" or "akali metals" or "Akali Metals":
    print(alkali())
else:
    print("Element not recognised")

现在我的问题是,无论我为userchoice变量投入什么,结果总是打印(氦)变量,我的意思是即使我放入“氖”它仍然会打印氦气即使如果userchoice变量包含单词“Helium”或“helium”,则只应打印氦变量。它显示在下面的代码中:

userchoice = str("Neon")


if userchoice == "Helium" or "helium":
    print(helium)
elif userchoice == "Neon" or "neon":
    print(neon)

我想知道是否有人会这么好并且帮助我做到这一点以便程序实际工作,此时只是为了澄清无论输入,所有程序输出都是氦变量,其中包括:

helium = ("""Name: Helium
Atomic Mass: 4.002602 u ± 0.000002 u
Group: Noble Gasses""")

1 个答案:

答案 0 :(得分:0)

问题是由userchoice == "Helium" or "helium"之类的行分隔or引起的,python尝试读取两个完全不同的条件。在后面的例子中,条件只是helium,它总是返回true。因此,将所有行更改为userchoice == "Helium" or userchoice == "helium"。或者只使用userchoice.lower()