如何在while循环python中将输入与列表中的项匹配

2024-05-29 07:23:21 发布

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

我很难理解为什么当我输入的球员选择它继续循环,即使他们输入正确的战士类型。如果有人能帮忙,我将不胜感激。在

playerchoice = ""

confirmwarrior = ""

warriortype = ("swordsman", "wizard", "archer", "healer")

while playerselection != warriortype:

        playerselection = input("""

        please select the type of warrior you wish to be:

        - Swordsman
        - Wizard
        - Archer
        - Healer

        """).lower()
else:
    print("you have entered and incorrect Warrior, please try again")    

print("your chosen Warrior is",playerselection)

    while confirmwarrior != "y":

        confirmwarrior = input("""

        are you happy with this Warrior?
        stefan

        y - yes
        N - No

        """)
        confirmwarrior.lower()


print("you have Chosen to be", playerselection)

Tags: toyouinputhavebelowerprint球员
2条回答

我想这是你的状况

您将比较用户输入的字符串作为输入,它将始终是一个字符串,例如“剑客”,并比较它是否与包含多个字符串的元组(始终为true)相等。在

我想你要找的条件是

while playerselection not in warriortype:

当playerselection等于元组中的某个项时,它将变为false。在

以下是正确工作的修改后的代码:

playerchoice = ""
playerselection = '' # set the variable playerselection to an empty string before the while loop
confirmwarrior = ""

warriortype = ("swordsman", "wizard", "archer", "healer")

while not playerselection in warriortype: # This is where you're missing

        playerselection = input("""

        please select the type of warrior you wish to be:

        - Swordsman
        - Wizard
        - Archer
        - Healer

        """).lower()
else:
    print("you have entered and incorrect Warrior, please try again")    

    print("your chosen Warrior is",playerselection) # Fixed your indentation here

    while confirmwarrior != "y":

        confirmwarrior = input("""

        are you happy with this Warrior?
        stefan

        y - yes
        N - No

        """)
        confirmwarrior.lower()


print("you have Chosen to be", playerselection)

编辑:当你在选择了一个战士后回答“不”时,下面的代码会重新询问你想成为哪种类型的战士:

^{pr2}$

相关问题 更多 >

    热门问题