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

如何在while循环中将输入与列表中的项目相匹配-python,python,python-3.x,list,while-loop,Python,Python 3.x,List,While Loop,我很难理解为什么当我输入玩家的选择时,即使他们输入了正确的战士类型,它也会继续循环。如果有人能帮忙,我将不胜感激 playerchoice = "" confirmwarrior = "" warriortype = ("swordsman", "wizard", "archer", "healer") while playerselection != warriortype: playerselection = input(""" please sel

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

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)

我想是你的情况

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

我想你要找的条件是

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)
编辑:在选择战士后回答“否”时,以下代码会再次询问您希望成为哪种类型的战士:

playerchoice = ""
playerselection = ''
confirmwarrior = ""

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

while confirmwarrior != "y":

    playerselection = input("""

    please select the type of warrior you wish to be:

    - Swordsman
    - Wizard
    - Archer
    - Healer

    """).lower()

    if not playerselection in warriortype:
        print("you have entered and incorrect Warrior, please try again")
        continue

    print("your chosen Warrior is", playerselection)


    confirmwarrior = input("""

    are you happy with this Warrior?
    stefan

    y - yes
    N - No

    """)
    confirmwarrior.lower()


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

你能修好压痕吗?这也有点让人困惑,你的
,而玩家选择!=warriortype:
将返回一个错误,因为您尚未声明变量
playerselection
可以吗?我批准更改。我认为您对while-else循环在Python中的工作方式有一些基本的误解。尝试向上查找,这可能会回答一些问题。例如,您的意思是在while循环中使用
playerchoice
,您必须在
操作中使用
而不是
=,因为你想检查用户输入是元组中的一个字符串。我认为像
a not in b
这样的语句是无效的,你可以在b
中做
而不是
>>a=(“hello”,“hi”)>>>“foo”not in a True
对我来说很好,我忘了这一点。谢谢。嗨,谢谢你的帮助。第一部分已经解决了问题,而不是…在。第二部分是你对这位战士感到满意。当我输入no时,它似乎进入了一个循环,而没有返回到再次输入选项。你知道有没有办法让while循环返回到第一个循环,再次输入战士。感谢you@StefanTrinh您好,我已经编辑了我的工作版本的答案,希望这有帮助!谢谢,谢谢,我知道我错在哪里了。