Python Forum
Number Guessing Game - Need Help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Number Guessing Game - Need Help
#1
I am trying to create a program where the user chooses a number between 1-100 and the computer tries to guess it in as few tries as possible. I think I am close but still need some help. Any and all help is appreciated. The code i have so far is pasted below....

import random

print("In this game you will guess a number and the computer will try to guess it in as few tries as possible.")
number = int(input("\n\nEnter a number 1-100 --- "))
cNumber = random.randint(1, 100)
placeholder = cNumber
tries = 1
print(number, cNumber, tries, placeholder)

print("\nThe computer guessed ",cNumber)

while cNumber != number:
    if cNumber > number:
        cNumber = random.randint(1,placeholder)
        tries += 1
        print("\nThe computer guessed",cNumber)
    if cNumber < number:
        cNumber = random.randint(placeholder, 1)
        tries +=1
        print("\nThe computer guessed",cNumber)

print("The computer guessed your number in this many tries --- ",tries)
Reply
#2
Hi I am new to python. I am trying to create a program where the user inputs a number and the computer guesses it in as few tries as possible. This is what I have so far. I am stuck. (Side Note... I am unsure why when I posted my code to this forum that it did not maintain the indentation.)


import random

print("In this game you will guess a number and the computer will try to guess it in as few tries as possible.")
number = int(input("\n\nEnter a number 1-100 --- "))
cNumber = random.randint(1, 100)
placeholder = cNumber
tries = 1
print(number, cNumber, tries, placeholder)

print("\nThe computer guessed ",cNumber)

while cNumber != number:
if cNumber > number:
cNumber = random.randint(1,placeholder)
tries += 1
print("\nThe computer guessed",cNumber)
if cNumber < number:
cNumber = random.randint(placeholder, 1)
tries +=1
print("\nThe computer guessed",cNumber)

print("The computer guessed your number in this many tries --- ",tries)
Reply
#3
Sorry please disregard this post. I reposted it correctly.
Reply
#4
I've merged your threads and added code tags to your original post. (Please don't make second threads when the topic is the same.) The reason your second post had no indentation appears to be because you copied from the first, in which the indentation was lost.
Reply
#5
Quote:I think I am close but still need some help.
Could you be more specific? What problem are you having, exactly?
Reply
#6
Sorry about that. Thanks for the help. Specifically, I need help with fixing the code. When I run it, it gets an error because the program guesses past the number and goes out of the parameters I set for the random number. I am not sure how to fix it. I am stuck. I am reading a Python book and one of the challenges for the chapter was to create this program.
Reply
#7
Here's some example output when I run your script
Output:
$ python tesit.py In this game you will guess a number and the computer will try to guess it in as few tries as possible. Enter a number 1-100 --- 50 (50, 66, 1, 66) ('\nThe computer guessed ', 66) ('\nThe computer guessed', 15) Traceback (most recent call last): File "tesit.py", line 18, in <module> cNumber = random.randint(placeholder, 1) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 242, in randint return self.randrange(a, b+1) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 218, in randrange raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width) ValueError: empty range for randrange() (66,2, -64)
(Ideally you would have provided this, and saved me the step of creating a file and running it.)

From this, we can narrow your problem to a specific line (18).
cNumber = random.randint(placeholder, 1)
You're calling randint in a way such that you'll always always have this problem as soon as this line is run (unless placeholder happens to be 1).
Output:
>>> random.randint(2, 1) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 242, in randint return self.randrange(a, b+1) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 218, in randrange raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width) ValueError: empty range for randrange() (2,2, 0) >>> random.randint(1, 1) 1
You should start your investigation there.
Reply
#8
Thanks for your help.
Reply
#9
Just use two points and add them and divide them by two and that's your first guess, just like a human would do. 0+100//2=50. Then an input would be if the guess is too high or low. Then the guess would be either 25 or 75 etc. Mathematically speaking, this should take about 8-10 steps, which is probably the quickest; unless you're randomly guessing or brute forcing.

Here's something I came up with quickly:
h=100
l=0
answer=""
while answer != "yes":
	m=(h+l)//2
	print("Is it",m,"?")
	answer=input("lower, higher or yes?")
	if answer=="lower":
		h*=1.5
	elif answer=="higher":
		h*=0.5
Though I'm not sure on it's reliability, just it's idea.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Beginner Boolean question [Guessing game] TKB 4 2,226 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Unable to count the number of tries in guessing game. Frankduc 7 1,842 Mar-20-2022, 08:16 PM
Last Post: menator01
  Guessing game problem IcodeUser8 7 3,518 Jul-19-2020, 07:37 PM
Last Post: IcodeUser8
  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
  Python Help - Guessing Game JamieT 5 2,681 Apr-16-2020, 01:30 PM
Last Post: deanhystad
  Guessing game kramon19 1 2,128 Mar-25-2020, 04:17 AM
Last Post: deanhystad
  Help for guessing game code Kronos 5 3,216 Mar-09-2020, 04:53 PM
Last Post: snippsat
  guessing the number game go127a 6 4,774 Apr-27-2019, 01:23 PM
Last Post: go127a
  Guessing Game does not work the_entrepreneur 3 2,758 Apr-20-2019, 06:19 AM
Last Post: SheeppOSU
  Generating number of objects for a game kom2 3 2,548 Apr-18-2019, 02:04 PM
Last Post: dan789

Forum Jump:

User Panel Messages

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