我无法让我的if / else语句运行else语句

时间:2019-01-27 20:43:59

标签: python if-statement

我写了下面的代码。每当我输入不可接受的值(例如a,b,c或d)时,我都希望不要嵌套运行else语句,但是每次我这样做时,它只会运行Instagram输入。我想知道如何解决这个问题。谢谢。

if commandPrompt.lower() == 'a' or 'b' or 'c' or 'd':
    instagram = input("Will you share this on Instagram? (y/n): ")
    if instagram.lower() == 'y':
        print()
        print("Thanks! You will get 5 knuts off your purchase!")
    else:
        print("Ok. No problem.")
else:
    print("You have entered an invalid selection. Please try again.")

1 个答案:

答案 0 :(得分:1)

您的状况的评估方式如下:

if (commandPrompt.lower() == 'a') or 'b' or 'c' or 'd':

始终为True。尝试这样的事情:

if commandPrompt.lower() in ['a', 'b', 'c', 'd']:
相关问题