Reputation: 3
I am learning Python and am working on a simple number guessing game. Basic premise is use is prompted to enter a number and then is told the guess is either too high, too low or correct. Once correct number is guessed, user is asked if they want to play again. However, when I type y the program exits back to prompt instead of re-starting. I've tinkered with the user prompts and the while and if portions and everything works EXCEPT for the part that is supposed to happen when user types y to play again.
This is the code I have:
import random
def playGame():
number = random.randrange( 1, 11 )
return number
chance = playGame()
print "\nI am thinking of a number between 1-10."
print "Can you guess my number?"
guess = int( raw_input( "Enter your guess. " ) )
while guess != chance:
if guess > chance:
print "Too high. Try again."
guess = int( raw_input ("\nEnter your guess. " ) )
if guess < chance:
print "Too low. Try again."
guess = int( raw_input ("\nEnter your guess. " ) )
if guess == chance:
print "Congratulations! You guessed my number!"
answer = raw_input( "\nWould you like to try again? (y or n) " )
if answer == 'n':
print "\nThank you for playing!"
if answer == 'y':
playGame()
If I type n the string correctly prints out. But when I click y the program simply exits. I've searched online but everything I have found says I simply need to just type functionName() and it should cycle back to the function and repeat but it's not working for me. There's no maximum number of tries to guess the number. I have set it to keep guessing until correctly guessed.
What am I missing?
Thanks!
Upvotes: 0
Views: 173
Reputation: 1705
You need to put the whole game into the playGame() function:
import random
def playGame():
chance = random.randrange(1, 11)
print "\nI am thinking of a number between 1-10."
print "Can you guess my number?"
guess = int( raw_input( "Enter your guess. " ) )
while guess != chance:
if guess > chance:
print "Too high. Try again."
guess = int( raw_input ("\nEnter your guess. " ) )
if guess < chance:
print "Too low. Try again."
guess = int( raw_input ("\nEnter your guess. " ) )
print "Congratulations! You guessed my number!"
playGame()
keep_playing = True
while keep_playing:
answer = raw_input( "\nWould you like to try again? (y or n) " )
if answer == 'n':
print "\nThank you for playing!"
keep_playing = False
if answer == 'y':
playGame()
Otherwise you just reset the random number - the rest of the file isn't automatically run again, only the function body.
Upvotes: 3
Reputation: 13767
I think you might be misunderstanding how the return
statement behaves in Python. playGame()
simply returns a random number. After you hit the return
statement in that function, your code returns back to where it was called from, which is the bottom of your script.
So, the flow kind of looks like this. We start here:
if answer == 'y':
playGame() # we enter the playGame() function
Then go here:
def playGame():
number = random.randrange( 1, 11 )
return number # We get here, and return BACK to where we were called from
Now back to where we called from:
if answer == 'y':
playGame()
# The program continues...but nothing to execute here, so terminate
srowland's answer will only allow you to repeat once. A common pattern to these "repeat" problems is to wrap all of the code that needs to be repeated into a function call. Then, when the condition for repeating is met, just call that function. If you want to repeat indefinitely until the user enters 'n', then you probably want this:
import random
def playGame():
number = random.randrange( 1, 11 )
return number
def start_my_game():
chance = playGame()
print "\nI am thinking of a number between 1-10."
print "Can you guess my number?"
guess = int( raw_input( "Enter your guess. " ) )
while guess != chance:
if guess > chance:
print "Too high. Try again."
guess = int( raw_input ("\nEnter your guess. " ) )
if guess < chance:
print "Too low. Try again."
guess = int( raw_input ("\nEnter your guess. " ) )
if guess == chance:
print "Congratulations! You guessed my number!"
answer = raw_input( "\nWould you like to try again? (y or n) " )
if answer == 'n':
print "\nThank you for playing!"
if answer == 'y':
start_my_game() # Start the game again
# Start the game
start_my_game()
Upvotes: -1