Hi, <br>your guess still use random.randrange that's make computer doesn't care about whether guess is low or higher than your number.<br><br>This code should be like this:<br> print "\t\t\tWelcome to \"Guess My Number\"!"
<br> print "\nThink of a number between 1 and 50."<br> print "I will try to guess it in as few attempts as possible.\n"<br><br> number = input ("Enter the number: ")<br> <br> #the computer guesses the number using the random function
<br> guess = random.randrange (50) + 1<br> tries = 1<br> <br> #FIXME<br> while (guess != number):<br> if (guess > number):<br> print "You chose", guess, "the number is Lower ..."
<br> guess = random.randint(1, guess-1)<br> else:<br> print "You chose", guess, "the number is Higher ..."<br> guess = random.randint(guess+1, 50)
<br> tries += 1<br> <br> print "You guessed it! The number was", number<br> print "And it only took you", tries, "tries!\n"<br><br> raw_input ("Press <ENTER> to exit.")
<br><br>Hope this help<br><br>pujo<br><br><div><span class="gmail_quote">On 12/15/05, <b class="gmail_sendername">William Mhlanga</b> <<a href="mailto:reddazz@gmail.com">reddazz@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I have been trying to write a guess my number game (using Michael Dawsons book), where the computer guesses the number that I thought of. Here is my code so far,<br><div style="text-align: left;">#The Guess My Number Game
<br>
#<br>#The computer picks a random number between 1 and 50<br>#The player tries to guess it and the computer lets<br>#the player know if the guess is too high, too low<br>#or right on the money<br>#If the player fails to guess the number after 5 tries
<br>#the game ends and the computer prints a chastising message<br>#<br><br>print "\t\t\tWelcome to \"Guess My Number\"!"<br>import random<br><br>print "\nThink of a number between 1 and 50."
<br>print "I will try to guess it in as few attempts as possible.\n"<br><br>number = input ("Enter the number: ")<br><br>#the computer guesses the number using the random function<br>guess = random.randrange
(50) + 1<br>tries = 1<br><br>#FIXME<br>while (guess != number):<br> if (guess > number):<br> print "You chose", guess, "the number is Lower ..."<br> else:<br> print "You chose", guess, "the number is Higher ..."
<br> guess = random.randrange (50) + 1<br> tries += 1<br><br>print "You guessed it! The number was", number<br>print "And it only took you", tries, "tries!\n"<br><br>raw_input ("Press <ENTER> to exit.")
<br><br>The program works ok, but the computer sometimes repeats the same numbers when asked to guess. How can I rewrite it so that it guesses like what a human being does i.e. if the number is less than 20, you do not guess numbers above 20.
<br><br>Thanks for your help.<br></div>
<br>_______________________________________________<br>Tutor maillist - <a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:Tutor@python.org">Tutor@python.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">
http://mail.python.org/mailman/listinfo/tutor</a><br><br><br></blockquote></div><br>