By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,767 Members | 1,098 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,767 IT Pros & Developers. It's quick & easy.

High Low game

P: 2
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:

Expand|Select|Wrap|Line Numbers
  1. # Guess My Number
  2. # The computer picks a random number
  3.  
  4. def begin ():
  5.  import random
  6. print "\tWelcome to 'Guess my Number'!:"
  7. print "\nI'm thinking of a number between 1 and 100."
  8. print "Try to guess it in as few attempts as possible.\n"
  9.  
  10. def guess_num(num, max_guesses):
  11.     # guessing loop
  12.     tries = 0
  13.     while True:
  14.         guess = int(raw_input("Take a guess: "))
  15.         tries += 1
  16.         if guess > num:
  17.             print "Lower..."
  18.         elif guess < num:
  19.             print "Higher..."
  20.         else:
  21.             print "You guessed it! The number was", num
  22.             print "And it only took you %d tr%s!\n" % (tries, ['ies', 'y'] [tries==1 or 0])
  23.             return
  24.  
  25.         if tries == max_guesses:
  26.             print "You have had %d guesses. Game over." % max_guesses
  27.             return
  28.         play = raw_input("Do you want to play again? Y/N")
  29.         if play == "Y":
  30.              begin ()
  31.         if play == "N":
  32.              print "Well you are no fun. Bye."
  33.  
  34.  
  35.  
  36.  
  37. the_number = random.randrange(1,100)
  38. max_guesses = 5
  39. guess_num(the_number, max_guesses)


Any help would be greatly appreciated.
Feb 13 '12 #1
Share this Question
Share on Google+
2 Replies

Smygis
100+
P: 126
begin() never calls guess_num().
Feb 13 '12 #2

P: 2
Okay so that is where I went wrong. Thank you for the help.
Feb 13 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.