为什么我的if条件始终不执行,而elif or else条件却不执行?

时间:2019-11-06 12:19:31

标签: python if-statement

我无法理解这个特定的if..else声明:

print("Do you want to run the next bit of code? Y/N")
n = input()

if n == "Y" or "y":
    j = [12, 43, 54, 65]
    for i in j:
        print (i, end="" "\t")

elif n == "N" or "n":
    print("You get numbers if you had pressed Y")

else:
    print("That's not an option")

我的问题是,无论我给n赋什么值,它总是给我Yy的输出。我总是得到输出作为数组编号。这意味着我的状况不正常。那么第一个条件实际上是什么问题呢?

1 个答案:

答案 0 :(得分:0)

选中this。 以下代码有效。

print("Do you want to run the next bit of code? Y/N")
n = input()
if n in ["Y", "y"]:
    j = [12, 43, 54, 65]
    for i in j:
        print (i, end="" "\t")
elif n in ["N", "n"]:
    print("You get numbers if you had pressed Y")
else:
    print("That's not an option")
相关问题