简体   繁体   中英

how to match an input to a item in a list within a while loop - python

I am struggling to understand why when i input the players choice it keep looping even if they type the right warrior type. would be most grateful if anyone can help.

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)

I think what is going on is your while condition

You are comparing the string entered by the user as input which will always be one string such as "swordsman" and comparing to see if it is not equal with a tuple containing multiple strings which will always be true.

I think the condition you are looking for would be

while playerselection not in warriortype:

which will become false when playerselection is equal to one of the items in your tuple.

Here's the modified code that works properly:

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)

EDIT: Here's the code that re-asks you what type of warrior you want to be when you answered no after choosing a warrior:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM