我的while语句有点古怪,我不知道怎么了

2024-04-28 11:51:02 发布

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

我的代码不起作用,我不知道发生了什么。问题是,当我第一次输入“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

Tags: oftoyouyourgetifoutprint
1条回答
网友
1楼 · 发布于 2024-04-28 11:51:02

您需要更改while循环语句。你知道吗

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

while outofbed != 1 and outofbed != 2:

希望这有帮助

相关问题 更多 >