"Guess the Number" game - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: "Guess the Number" game (/thread-4016.html) Pages:
1
2
|
RE: "Guess the Number" game - CTT - Jul-24-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") RE: "Guess the Number" game - ichabod801 - Jul-24-2017 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 = minimumThe 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
RE: "Guess the Number" game - CTT - Jul-24-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) RE: "Guess the Number" game - ichabod801 - Jul-24-2017 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. RE: "Guess the Number" game - sylas - Jul-26-2017 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) |