我的声明很古怪,我不确定是什么问题

时间:2016-05-26 19:15:19

标签: python python-3.x while-loop python-3.1

我的代码无效,我不知道发生了什么。问题是当我输入" Y"第一次,它仍然说"请输入有效的输入"我需要再次进入第二次才能工作。

if getoutofbed == "y":
    outofbed = 1
    print("You chose to get out of bed.")
elif getoutofbed == "n":
    outofbed = 2
    print("Your mom disregards your choice and screams at you to get out of bed. Listening to your mother, you step off the ledge of your resting place.")
    print("    .@@@@,")
    print("    aa`@@@,",name.upper(),"I DONT CARE IF YOU DONT WANNA GET UP!")
    print("    =  `@@@ GET UP!")
    print("      )_/`@'")
    print("     / || @")
    print("     | || @")
    print("     /~|| `")
    print("    /__W_/")
    print("      |||")
    print("     _|||")
    print("    ((___)")
    print("")
    print("")
while outofbed != 1 or 2:
    print("Please enter a valid input.")
    print("")
    print("")
    getoutofbed = input("Choose to get out of bed? Y/N").lower()
    print("")
    print("")
    if getoutofbed == "y":
        print("You chose to get out of bed.")
        break
    elif getoutofbed == "n":
        print("    .@@@@,")
        print("    aa`@@@,",name.upper(),"I DONT CARE IF YOU DONT WANNA GET UP!")
        print("    =  `@@@ GET UP!")
        print("      )_/`@'")
        print("     / || @")
        print("     | || @")
        print("     /~|| `")
        print("    /__W_/")
        print("      |||")
        print("     _|||")
        print("    ((___)")
        break

1 个答案:

答案 0 :(得分:2)

您需要更改while循环语句。

即使outofbed != 1评估为False2也会评估为True,因为它是一个“真实”值。将其更改为:

while outofbed != 1 and outofbed != 2:

希望这有帮助