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

Michael Dawson's Guess My Number

P: 3
I am trying to duplicate the Guess My Number Game from Python Programming for the Absolute Beginner by Michael Dawson but for some reason I still cannot figure out my output is not completely correct.
The goal of the program is from the computer to pick a number between 1 and 100 and the user to try to guess the number...if the user choses a number that is too high, the computer will tell the user that their guess is too large and if the number is too low the computer will tell the user that their guess is too small and if the guess is correct, the computer should tell the user "You correctly guessed the number that the computer selected ", print the number and display the number of tries....Can you please help?

Expand|Select|Wrap|Line Numbers
  1. import random
  2. print "\tWelcome to 'Guess My Number' !"
  3. print "\nI'm thinking of a number between 1 and 100."
  4. print "Try to guess it in as few attempts as possible.\n"
  5. the_number = random.randrange(100) + 1
  6. guess = int(raw_input("Take a guess: "))
  7. tries = 1
  8. while (guess != the_number):
  9.             if (guess > the_number):
  10.                 print "The number you guessed is too large"
  11.                 guess = int(raw_input("Take a guess: "))
  12.                 tries +=1
  13.             else:
  14.                 print "The number you guessed is too small"
  15.                 guess = int(raw_input("Take a guess: "))
  16.                 tries +=1
  17.                 print "You correctly guessed the number that the computer    selected ", the_number
  18.                 print "And it only took you", tries, "tries!\n"
  19. raw_input("Press enter to exit")
Oct 18 '08 #1
Share this Question
Share on Google+
2 Replies

Expert Mod 2.5K+
P: 3,503
Almost on the code tags, the \ should be a / on the closing tags.
Oct 18 '08 #2

Expert 100+
P: 1,247
Since you're learning, I'm only going to give you hints, because people usually learn better if they discover things themselves.
Hint1: Indents decide what code is part of what codeblock.
Hint2: "elif" would be quite handy here.

Try following the execution flow in your mind, and you'll find out what's wrong.
Oct 18 '08 #3

Post your reply

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