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()