jeudi 17 septembre 2020

Python: Checks whether the user input matches any item list; keep asking until get it right

I am newbie, and I need help, please. I am asking the user to guess and matches any item in a pre-defined list, if it's not correct - keeps asking until the user gets it right once. However, I am trying to do many ways but cannot get the code right. Sometimes, it's just ask 1 time and stop even if the user input is wrong; or it does not recognize the answer right or wrong and keep asking. Thank you!

animal = ['bird','dog','cat','fish']
while True:
    guess = input('Guess my favorite animal: ')
    if guess == animal:
        print("You are right")
        break
    print('Try again!')

Aucun commentaire:

Enregistrer un commentaire