0

I am learning python right now and there is guessing game within the tutorial. You have to guess a word and you have 3 tries until you fail. Now I want to change the code that the word you have to guess is a part of a list not a single value password = ["123456", "qwerty", "password"] instead of password = "123456"

I tried changing while userInput != password to while userInput != password[0-2] but only the secound item from the list gives me correct then. Can you help me please?

password = ["123456", "qwerty", "password"]
userInput = ""
count = 0
limit = 3
out_of_guesses = False

while userInput != password and not(out_of_guesses):
    if count < limit:
        userInput = input("Enter guess:")
        count = count + 1
    else:
        out_of_guesses = True

if out_of_guesses:
    print("You failed")

else:
    print("Correct!")
  • 1
    `while userInput not in password and not out_of_guesses:` The operator `in` can be used to check if something is a member of a collection like a list, and `not in` does the opposite. – Green Cloak Guy May 20 '21 at 14:03
  • change `userInput != password` to `userInput not in password` – oskros May 20 '21 at 14:03
  • password[0-2] is a wrong syntax! That is the 2nd element of password from the end. – Yaroslav Nikitenko May 20 '21 at 14:05
  • To be precise, what the other comments are suggesting will result in all passwords being "valid" - I don't think that is what you were asking for. If you want to pick one (and only one) of the passwords to be the "valid" one, you could use `random.choice` from the `random.module`. – Paul M. May 20 '21 at 14:25

0 Answers0