Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Guess the Number" game
#11
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")
Reply
#12
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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
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)
Reply
#14
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#15
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)

        
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 2,958 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  Unable to count the number of tries in guessing game. Frankduc 7 1,842 Mar-20-2022, 08:16 PM
Last Post: menator01
  Ask again if wrong guess with While. EduPy 4 2,209 Oct-21-2021, 07:46 PM
Last Post: menator01
  I guess it's about print tsavoSG 2 2,073 Feb-08-2021, 08:34 PM
Last Post: steve_shambles
  Beginner Code, how to print something after a number of turns (guessing game) QTPi 4 2,679 Jun-18-2020, 04:59 PM
Last Post: QTPi
  guessing the number game go127a 6 4,774 Apr-27-2019, 01:23 PM
Last Post: go127a
  Generating number of objects for a game kom2 3 2,548 Apr-18-2019, 02:04 PM
Last Post: dan789
  can't figure out problem with number guess Galoxys 4 3,291 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Guess a number Ameen 5 13,066 Apr-03-2018, 01:20 PM
Last Post: snippsat
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,094 Jan-07-2018, 04:26 AM
Last Post: squenson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020