I am trying to write a program in Python for a High Low guessing game. It has to tell the person if their guess is higher or lower, also keep track of their guesses, it also needs to ask them if they would like to play again and loop back to the beginning when done. I have most of it done but I am having problems with making it play again. This is what I have so far:
Any help would be greatly appreciated.
Code:
# Guess My Number
# The computer picks a random number
def begin ():
import random
print "\tWelcome to 'Guess my Number'!:"
print "\nI'm thinking of a number between 1 and 100."
print "Try to guess it in as few attempts as possible.\n"
def guess_num(num, max_guesses):
# guessing loop
tries = 0
while True:
guess = int(raw_input("Take a guess: "))
tries += 1
if guess > num:
print "Lower..."
elif guess < num:
print "Higher..."
else:
print "You guessed it! The number was", num
print "And it only took you %d tr%s!\n" % (tries, ['ies', 'y'] [tries==1 or 0])
return
if tries == max_guesses:
print "You have had %d guesses. Game over." % max_guesses
return
play = raw_input("Do you want to play again? Y/N")
if play == "Y":
begin ()
if play == "N":
print "Well you are no fun. Bye."
the_number = random.randrange(1,100)
max_guesses = 5
guess_num(the_number, max_guesses)
Any help would be greatly appreciated.
Comment