# This time, the user picks a number and the computer tries to guess it.
import random
number = int(raw_input("Pick a Number between 1 and 100: "))
print "\n\nThe computer will now try to guess your number."
print "Respond by typing 'higher' or 'lower', be honest!\n\n"
cpu_guess = random.randrange(100) + 1
x = cpu_guess
print "Computers guess is ", cpu_guess
while cpu_guess != number:
answer = raw_input("\n\nIs your number 'higher' or 'lower'? ")
if answer == "higher":
cpu_guess = random.randrange(x, 100) + 1
elif answer == "lower":
cpu_guess = random.randrange(0, x) + 1
print "\nComputers Guess is ", cpu_guess
print "The computer has guessed your number, your number was: ", number
python guess # program help
Hi , Ive made guess the # programs before, but now Ive started a new book and one of teh exercises is to make a program where the user chooses a number
1 - 100 and the computer guesses. The book wants me to use the randrange() function. I have the program working decently, but the problem is when the user answers higher or lower. When they type higher or lower, the computers guess range gets changed, as you can see in the code. To explain the problem, I will give an example. Lets say the number chosen is 45. If the computer guesses 75, the user says lower and now the computer generates a random between 0 and 74. Thats fine. But hers the problem. Lets say the next guess is 17, the user says higher, and the computers range of guesses changes again, this time its between 18 - 100. So you see, The new range doesnt take into account that we already know that the number isnt higher than 75. I need to make it so that the range stays between the higher and lower responses. I hope I am being clear. Heres the code:
Have the program store the min and max range of the guess based on the answers given by the user. Using the min and max values for the range in next random guess.
I dont want to be annoying, but can someone just repost my code with the necessary changes to accomplish what Im trying to do? Im not trying to cheat, but Ive been going crazy trying to figure this out and Im not making any progress. If I can see how its done I would learn alot more.
Never written in Python before:
cpu_guess = random.randrange(100) + 1min_guess_range = 0max_guess_range = 100print "Computers guess is ", cpu_guesswhile cpu_guess != number: answer = raw_input("\n\nIs your number 'higher' or 'lower'? ") if answer == "higher": min_guess_range = cpu_guess elif answer == "lower": max_guess_range = cpu_guess cpu_guess = random.randrange(min_guess_range, max_guess_range) + 1 print "\nComputers Guess is ", cpu_guess
Well I just had to add the users input for a number to your code, but otherwise it seems to be working perfectly. Im going to study it a bit to make sure it works in all situations. Thanks so much though, Im gonna work with your code to learn whats going on a bit better... thanks again dude, not bad for someone who never used python :)
The code block wasn't the full code, it was just the section of your code that I modified.
The 'full' code would look like this:
The 'full' code would look like this:
# This time, the user picks a number and the computer tries to guess it.import randomnumber = int(raw_input("Pick a Number between 1 and 100: "))print "\n\nThe computer will now try to guess your number."print "Respond by typing 'higher' or 'lower', be honest!\n\n"cpu_guess = random.randrange(100) + 1min_guess_range = 0max_guess_range = 100print "Computers guess is ", cpu_guesswhile cpu_guess != number: answer = raw_input("\n\nIs your number 'higher' or 'lower'? ") if answer == "higher": min_guess_range = cpu_guess elif answer == "lower": max_guess_range = cpu_guess cpu_guess = random.randrange(min_guess_range, max_guess_range) + 1 print "\nComputers Guess is ", cpu_guess print "The computer has guessed your number, your number was: ", number
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement