0

I'm experimenting with the basic guessing game script that gets shown to you as part of "Automate The Boring Stuff With Python".

I want the game to loop infinitely until someone says anything other than "Yes" or "yes" in response to "Would you like to try again?" at which point I would like the loop to break and the script to die. However, even if someone inputs something else, the script always behaves as if "Yes" has been inputted in response. I have no idea why.

Here's the code:

#Guess the number game

import random

def the_game(): # This is the game itself
    try:
        global secretNumber
        global guessesTaken
        global guess
        global try_again
        secretNumber = random.randint(1,20)
        
        for guessesTaken in range(1, 7):
            print("Take a guess.")
            guess = int(input())

            if guessesTaken - 6 == 0:
                print("You have " + str(6 - guessesTaken) + " attempts remaining. Boo.")
            elif guess < secretNumber:
                print("Try again. Higher this time. You have " + str(6 - guessesTaken) + " attempts remaining.")
            elif guess > secretNumber:
                print("Nope. Less. You have " + str(6 - guessesTaken) + " attempts remaining.")
            else:
                break
    except ValueError:
        print("Please enter a number.")
        the_game()
    if guess == secretNumber:
        if guessesTaken == 1:
            print("You did it in " + str(guessesTaken) + " attempt. I bet you're proud of yourself.")
        else:
            print("You did it in " + str(guessesTaken) + " attempts. I bet you're proud of yourself.")
    else:
        print("It was " + str(secretNumber) + ". You suck " + name + ".")

    print("Would you like to try again? Yes or no.")
    try_again = input()



print("Hello. What is your name?")
name = input()
print("Well, " + name + ". I am thinking of a number between 1 and 20")


while True:
    the_game()

## I want the game to loop infinitely until someone says anything
## other than "Yes" or "yes" in response to "Would you like to try again?"
## at which point I would like the loop to break and the script to die.

## However, even if someone inputs something else, the script always behaves
## as if "Yes" has been inputted in response. I have no idea why.
    
    if try_again == "Yes" or "yes":
        print("Here we go again.")
        the_game()
    else:
        print("Goodbye!")
        break
        exit()
  • 3
    `if try_again.lower() == "yes"` – azro Jun 07 '21 at 21:06
  • https://pastebin.com/YJLirnki take the time to read it slowly, there is many improvements ;) – azro Jun 07 '21 at 21:14
  • Don't use global variables, their value is hard to control, good practice is to use local variables always when possible, and global only when you are really know what you are doing. Try to return `try_again` variable from `the_game()` function – Mateusz Dorobek Jun 07 '21 at 21:18
  • `if try_again == "Yes" or try_again == "yes":` or `if try_again in ("Yes", "yes"):` or the simplest `if try_again.lower() == "yes":` – furas Jun 07 '21 at 21:47

0 Answers0