Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Guess the Number" game
#1
Hi I'm new to the forum and a beginner to python, please take it easy :) I'm trying to do "guess the number" game, but couldn't solve it

The program will generate number from 1 to 99 and I have to input the answer first then input my guess. If my guess(ex:50) it larger than the answer(ex:45), then the output will show a range and sentence "wrong answer, guess smaller" 1<?<50. If my next attempt(ex:30) is too small, the output will show a new range and sentence "wrong answer, guess larger"  30<?<50. If I guess right, then the output will show "bingo answer is X(my guess)"

The below is what I have right now. Please help me fix it. Thanks!
import random

min=1
max=99
random.randint(1,99)
correct_answer = int(input())
print("1<?<100")
guess= int(input())

while guess != correct_answer:
   more_attempts=int(input())
   if guess>correct_answer:
       print("wrong answer, guess smaller","{0}<?<{1}".format(min,max),sep='\n')
   elif guess<correct_answer:
       print("wrong answer, guess larger", "{0}<?<{1}".format(min,max),sep='\n')
   else:
       guess == correct_answer
       print("bingo answer is",guess)
       break
Reply
#2
In the future, please use python tags. See the BBCode link in my signature for instructions on how to do that.

First, you are asking twice without giving feedback, with the guess line before the loop, and the more_attempts line at the top of the loop. Second, your input within the loop is assigned to more_attempts, so the value of guess never changes in the loop, and you can't get out of it. I would eliminate the guess input outside the loop, and rename more_attempts to guess. This will cause an error with the condition in your loop. However, you don't need that condition, since you break when they get the right answer at the bottom of the loop. I would just make it a while True loop.

Finally, you need to update min and max when they guess incorrectly. When the guess is higher than the correct answer, update max to the guess. When the guess in lower than the correct answer, update min to the guess.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Quote:random.randint(1,99)
Think of what happens here.
>>> import random
>>> random.randint(1,99)
37
# Then number is gone not stored in variable.
>>> secret_number = random.randint(1,99)
>>> secret_number
61
So to start.
import random

secret_number = random.randint(1,100)
tries, guess = 0, 0
while guess != secret_number:
   guess = int(input("Take a guess: "))
Will loop and ask question as long as secret_number is not guessed.
Need if elif eg if guess > secret_number: lower... and higher... print() to user.
Reply
#4
Thank you guys for helping!
But what I meant is I have to input the secret_number(which is the answer in this case), so I shouldn't use secret_number=random.randint(1,99). I'm still having trouble writing the code to update the max and min. Can anyone help? Thanks!
Reply
#5
Updating max and min should be easy. You just need to assign new values to them when you tell them it's a wrong answer, like I said in my first post.

Show us the code where you're trying to update them, and we'll help you fix it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
I updated the new max and min with a new loop, however, seems like I complicated this code...it will give me two results after the third attempt....please help. Thanks!

import random

random.randint(1,99)
answer = int(input())
print("1<?<100")
guess = int(input())

while True:
   if guess ==answer:
       print("bingo answer is",answer)
       break
   if guess<answer:
       print("wrong answer, guess larger")
       maximum=max(100,guess,answer)
       minimum=min(100,guess,answer)
       print(minimum,"<?<",maximum)
       new_guess = int(input())
       if answer<new_guess:
           print("wrong answer, guess smaller")
           new_max=max(answer,guess,new_guess)
           new_min=min(answer,guess,new_guess)
           print(new_min,"<?<",new_max)
       elif new_guess<answer:
           print("wrong answer, guess larger")
           new_max=max(answer,guess,new_guess)
           new_min=min(answer,guess,new_guess)
           print(new_min,"<?<",new_max)            
       else:
           print("bingo answer is",answer)
           break
   if answer<guess:
       maximum=max(1,guess,answer)
       minimum=min(1,guess,answer)
       print(minimum,"<?<",maximum)
       print("wrong answer, guess smaller")
       new_guess = int(input())
       if new_guess<answer:
           print("wrong answer, guess larger")
           new_max=max(answer,guess,new_guess)
           new_min=min(answer,guess,new_guess)
           print(new_min,"<?<",new_max)
       elif answer<new_guess:
           print("wrong answer, guess smaller")
           new_max=max(answer,guess,new_guess)
           new_min=min(answer,guess,new_guess)
           print(new_min,"<?<",new_max)
       else:
           print("bingo answer is",answer)
           break
Reply
#7
Just to finish what i started,without random.
Then you can look at it an try to implement your max /min stuff.
secret_number = 34
tries, guess = 0, 0
while guess != secret_number:
   guess = int(input("Take a guess: "))
   if guess > secret_number:
       print("Lower...")
   elif guess < secret_number:
       print("Higher...")
   tries += 1
   
print('You guessed it! The number was {} in {} tries'.format(guess, tries))
Test:
Output:
C:\1_py\div λ python guess.py Take a guess: 34 You guessed it! The number was 34 in 1 tries C:\1_py\div λ python guess.py Take a guess: 50 Lower... Take a guess: 40 Lower... Take a guess: 20 Higher... Take a guess: 30 Higher... Take a guess: 34 You guessed it! The number was 34 in 5 tries
Reply
#8
You don't update both min and max each guess, you update the appropriate one. If they guess too high, they want to guess higher than that again, so you reset the max at that point. You reset the min when they guess too low. And you can pretty much just set it to the guess, without taking into account the other numbers. The only time you might want to set it to something besides the guess is when the user guesses higher than their previous highest guess. But in that case the user is an idiot.

And right now you're asking twice each loop. Just ask once in the loop, and loop will take care of asking again. So take out the two second layers of if/else blocks under the first set of if else blocks.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
Thanks guys! but I think I have to update both max and min when I input a new number (Sorry maybe what I said wasn't clear enough)
First I have to input the answer(let's say 45), and the output should be "1 < ? < 100"
Next I'll input my first guess(ex:50), and the output will be "wrong answer, guess smaller" "1<?<50"
Next I'll input my second guess(ex:30), and the output will be "wrong answer, guess larger" "30<?<50"
and so on, until I input the correct answer(which is 45), then the output will be "bingo answer is 45"

So as you can see, I have to reset my max and min every time I input my new guess. So I'm still figuring out how to set the max and min.
Reply
#10
(Jul-24-2017, 03:23 AM)CTT Wrote: Thanks guys! but I think I have to update both max and min when I input a new number (Sorry maybe what I said wasn't clear enough)
First I have to input the answer(let's say 45), and the output should be "1 < ? < 100"
Next I'll input my first guess(ex:50), and the output will be "wrong answer, guess smaller" "1<?<50"
Next I'll input my second guess(ex:30), and the output will be "wrong answer, guess larger" "30<?<50"

Um, you're contradicting yourself. When you input the first guess, you only update the max. The min stays at 1. When you input the second guess, you only update the min. The max stays at 50. Only one number is changing each time you make a guess.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Guess the word game help jackthechampion 3 3,020 Sep-06-2023, 06:51 AM
Last Post: Pedroski55
  Unable to count the number of tries in guessing game. Frankduc 7 1,903 Mar-20-2022, 08:16 PM
Last Post: menator01
  Ask again if wrong guess with While. EduPy 4 2,248 Oct-21-2021, 07:46 PM
Last Post: menator01
  I guess it's about print tsavoSG 2 2,110 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,740 Jun-18-2020, 04:59 PM
Last Post: QTPi
  guessing the number game go127a 6 4,838 Apr-27-2019, 01:23 PM
Last Post: go127a
  Generating number of objects for a game kom2 3 2,582 Apr-18-2019, 02:04 PM
Last Post: dan789
  can't figure out problem with number guess Galoxys 4 3,341 Oct-29-2018, 01:45 PM
Last Post: snippsat
  Guess a number Ameen 5 13,115 Apr-03-2018, 01:20 PM
Last Post: snippsat
  Guess Random Number Why i m not able to enter input Nithya Thiyagarajan 6 8,193 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