I have a game in which the user is meant to guess a number between 1-100 and it will run through the game, and when it gets to the end it asks the user if they want to play again. However, when the user says "yes", the game does not run again.
I have both the game defined and the ending defined in that order, but when the user says yes, the game will not play again, even though I have it so that the game is being called.
Code:
import random
import time
# Generate a random number between 1 and 100
secret_number = random.randint(1, 100)
# Set the maximum number of guesses
max_guesses = 5
# Initialize the number of guesses the player has made
guesses_made = 0
# Start the game loop
def game():
global secret_number
global max_guesses
global guesses_made
while guesses_made < max_guesses:
# Get the player's guess
guess = int(input("Enter your guess: "))
# Increment the number of guesses the player has made
guesses_made = 1
# Check if the player's guess is correct
if guess == secret_number:
print("You guessed the correct number! You win!")
break
# Check if the player's guess is too high
elif guess > secret_number:
print("Your guess is too high. Try again.")
# The player's guess must be too low
elif guess < secret_number:
print("Your guess is too low. Try again.")
# The player has run out of guesses
if guess != secret_number and guesses_made == max_guesses:
print("You have run out of guesses. The secret number was", secret_number)
def end():
print("Play again?")
user = input()
if user.casefold() == "yes":
print("okay!")
game()
elif user.casefold() == "no":
print("okay, goodbye!")
else:
print("That's not an option. Please say yes or no.")
time.sleep(1)
print ("Do you want to play again?")
user = input()
if user == "yes":
print("okay!")
print()
game()
elif user == "no":
print("okay, goodbye!")
# Explain the game to player
print ("Hello player, let's play a guessing game.")
time.sleep(1.5)
print ("I will pick a number between 1-100 and you have to try to guess it.")
time.sleep(1.5)
print()
print ("I will give you five chances to guess the number. If you guess correctly I will give you a treat. If at the end you still cannot guess the number then I will tell you and we will play again.")
time.sleep(2)
print()
print ("I hope you are ready!")
time.sleep(.5)
print ("Ready...")
time.sleep(.25)
print ("Set...")
time.sleep(.25)
print ("GUESS!!!")
print()
game()
end()
I have tried re-defining the game but then it just played the game without running the beginning. I have also tried making it global but then there are just errors due to the ()
.
CodePudding user response:
It's not that it's not working, it's that you've run out of guesses. You need to reinitialise the number of guesses and the random number each time you call game().
E.g. Rather than use global variables, make them local to the game() function and initialise them in there. They shouldn't be global anyway as they have no use outside of the game() function.
CodePudding user response:
The problem is with the global variable guesses_made.
There are a two scenarios that may allow you to have a clearer picture:
Scenario 1: The user uses up all the guesses.
The variable guesses_made is already 5, and once you run the function game()
again, it will go to the statement if guess != secret_number and guesses_made == max_guesses
. Hence the game will stop automatically.
Scenario 2: The user never uses up all the guesses.
Let's have the variable guesses_made = 2. (Just a random number, lucky player!) Then when you select "yes" to play again, you will be able to play again, but with 3 chances left. Because you didn't reset the variable to 0
Also a side note:
Because you don't have a loop in it, it will at most allow the player to play two times only.