简体   繁体   中英

I'm trying to create a guessing game but when I run my code, nothing happens

I'm currently new to programming so I'm trying to create a guessing game as practice but I don't know what I did wrong.

secret_word = "Giraffe" or "giraffe"
guess_word = ""
failed = False
guess_count_limit = 3
guess_count = 0

while guess_word != secret_word and not failed:
    if 1 < guess_count:
        guess = input("Guess the animal I'm thinking of: ")
        guess_count += 1
    elif 2 < guess_count:
        print("Incorrect but here's a hint: ")
        print("It's common in Africa. ")
        print("Two guesses left.")
        guess = input("Now try again: ")
        guess_count += 1
    elif 3 < guess_count:
        print("Incorrect but here's your last hint: ")
        print("It's tall. ")
        print("One guess left")
        guess = input("Now try again: ")
    elif guess_count == guess_count_limit:
        print("Nope, out of guesses.")
        failed = True

if failed:
    print("You win")
else:
    print("Wow, you really lost huh.")

When I try to run this program, nothing happens.

This version will work:

secret_word = "giraffe"
guess_word = ""
guess_count_limit = 3
guess_count = 0

while guess_word.lower() != secret_word and guess_count < guess_count_limit :
    if 1 > guess_count:
        guess_word = input("Guess the animal I'm thinking of: ")
        guess_count += 1
    elif 2 > guess_count:
        print("Incorrect but here's a hint: ")
        print("It's common in Africa. ")
        print("Two guesses left.")
        guess_word = input("Now try again: ")
        guess_count += 1
    elif 3 > guess_count:
        print("Incorrect but here's your last hint: ")
        print("It's tall. ")
        print("One guess left")
        guess_word = input("Now try again: ")
    elif guess_count == guess_count_limit:
        print("Nope, out of guesses.")
        print("Wow, you really lost huh.")

if guess_word.lower() == secret_word :
    print("You win")

you made 3 mistakes:

secret_word = "Giraffe" or "giraffe"
  1. As far as I know logical operators does not work with strings. However, you do not need 2 words with different letter cases, you need only one and then change the case of the guesses to check.

  2. when you took the input you stored in guess but in the loop you are comparing guess_word with secret_word!!

  3. you had a problem inside the loop in the condition statements, try to figure it out in order to learn and become better.

good luck!

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