如何让“While False”循环在python中工作?

2024-06-08 22:09:58 发布

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

我很难让python代码正常工作……我正在制作一个基于故事的游戏,我想检查用户是否输入了正确的类。 我的代码:

ClassPicked = False

#Player Stats
Energy = 1
Health = 1

print("Hello, welcome to this text based game")

while ClassPicked == False:
    print("Please choose a class : Warrior or Mage")
    classType = input()

    #Failsafe to stop non roles to be selected
    if classType != "Paladin" "Warrior" or "Mage":
        print("You need to select a role")

    if classType == "Paladin" "Warrior" or "Mage":
        ClassPicked = True


print("You have selected", classType)

我需要帮助,因为while循环不起作用,只需询问用户希望选择什么类,无论输入如何,代码都将继续


Tags: orto代码youfalseifpaladinprint
1条回答
网友
1楼 · 发布于 2024-06-08 22:09:58

or并不像你想象的那样工作。它只在两个完整表达式之间工作

要检查classType是否等于您的任何东西,您需要使用不同的策略:

if classType not in ["Paladin", "Warrior", "Mage"]:
    print("select a role")
else:  # classType is one of those things exactly
    classPicked = True

相关问题 更多 >