Posts: 6
Threads: 1
Joined: Jul 2017
Yeah you are right, and I updated my min to guess when it's smaller than answer, and max to guess when larger than answer. But now I have trouble updating the other half, when guess is smaller than answer, then the max should be max of the previous max; when guess is larger than answer, min should be min of the previous min. The below is what I have now
import random
random.randint(1,99)
answer = int(input())
maximum=100
minimum=1
print(minimum,"<?<",maximum)
while True:
guess = int(input())
if guess ==answer:
print("bingo answer is",answer)
break
elif guess<answer:
print("wrong answer, guess larger")
new_max=maximum
new_min=guess
print(new_min,"<?<",new_max)
elif answer<guess:
new_max=guess
new_min=minimum
print(new_min,"<?<",new_max)
print("wrong answer, guess smaller")
Posts: 4,220
Threads: 97
Joined: Sep 2016
Jul-24-2017, 02:54 PM
(This post was last modified: Jul-24-2017, 02:55 PM by ichabod801.)
The problem you have here is you are not keeping the new values. Each time through the loop you assign guess to the correct new_max or new_min, but then you overwrite the other one with the original value. Say the target is 81, and you guess 50, which is too low. You set new_min to 50 and new_max to 100. Then, say you guess 90, which is too high. You set new_max to 90 (good) but then resent new_min back to 1 (bad).
There are two ways to fix this. One is to remove new_max = maximum and new_min = minimum . Of course, this will give you a NameError, because the first time through the loop they will not have been assigned yet. So just before the loop, initialize those variables:
new_max = maximum
new_min = minimum The other solution would be to get rid of new_max and new_min, and to just modify maximum and minimum directly. So after a low guess, you would have minimum = guess and after a high guess you would have maximum = guess
Posts: 6
Threads: 1
Joined: Jul 2017
So I chose the second solution to get rid of new_max and new_min. However, I don't really know after a low guess, I have minimum=guess, but how about the maximum in this case (I guess I'll keep the previous max but I don't know what to assign for max), and and vice versa for the high guess.
This is what I have now, thanks a lot!
import random
random.randint(1,99)
answer = int(input())
print("1<?<100")
while True:
guess = int(input())
if guess ==answer:
print("bingo answer is",answer)
break
elif guess<answer:
print("wrong answer, guess larger")
minimum=guess
print(minimum,"<?<",maximum)
elif answer<guess:
maximum=guess
print("wrong answer, guess smaller")
print(minimum,"<?<",maximum)
Posts: 4,220
Threads: 97
Joined: Sep 2016
In the case of a low guess, the maximum doesn't change. When you guess low, all you learn is that your guess was too low, you don't get any information about what guesses are too high. So that maximum shouldn't change.
Posts: 382
Threads: 94
Joined: Mar 2017
Jul-26-2017, 11:41 AM
(This post was last modified: Jul-26-2017, 11:44 AM by buran.)
Never too late for good thinks
#game.py
import random
print("GUESS NUMBER >1 AND <100 ")
answer = random.randint(1, 99)
print(answer)
maximum = 100
maximum2=100
minimum = 1
new_min=1
new_max2=100
print(minimum, "<your number<", maximum)
while True:
guess = int(input())
if guess == answer:
print("bingo answer is", answer)
exit()
elif guess < answer:
print("wrong answer, guess larger")
new_max = guess
new_min = guess
print(guess, "<?<", new_max2)
elif answer < guess:
print("wrong answer, guess smaller")
new_max2 = guess
new_min2 = guess
print(new_min, "<?<", guess)
|