Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
guessing the number game
#1
I am going to write code in this condition:

I have a number in my mind and the code is going to find out, I have wrote it but it has a problem in while loop
if the guess from code is bigger I will reply 'b' or smaller :'k' and for the correct answer 'd'
import random
x = 1
y = 99
guess= random.randint(x,y)
print(guess)

results = input()
print(results)
while results != 'd':
    break
    print ('wooow , computer you did it! ')
    if results == 'b':
        
        guess= random.randint(guess,y)
        print (guess)
        results = input()
    elif results == 'k':
        
        guess= random.randint(x,guess)
        print (guess)
        results = input()
Reply
#2
Just a remark: 'computer' should not be so stupid that it guesses randomly - with binary search algorithm the worst-case scenario is 7 guesses from one hundred numbers.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
I know its just a Homework !!
Reply
#4
Yes, this is homework. Let's 'play' this game:

1. computer gives random number, you say that actual number is bigger;
2. computer gives another random number (which is bigger than in #1), you say that actual number is smaller

Now, the number is between two numbers guessed by computer in steps #1 and #2. Computers next guess should be between those boundaries, otherwise game doesn't make sense. Your code takes into account overall boundaries, last guess and last answer. So computers next random guess could be less than in step #1 (which we know is wrong) as only requirement for guess is to be smaller than in step #2 and to be within overall/starting boundaries.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Computer can guess between boundaries.
In addition, the boundaries should be updated after I tell that my first number is bigger or smaller.

For example I have 50 in my head.
Computer guess between 1 and 99. First output can be 12 from computer.
I will tell him that mine is bigger. So next guess from computer would be between 12 and 99.(new boundary).
Second guess as an example is 60. So I told that, mine number is smaller. Then next boundary would be between 12 and 60.
Until I told him the guessed number is okay its. ā€˜dā€™
Reply
#6
You need to rewrite your Python code to correspond with what you wrote in english.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
I know bu the problem is, How can I update the range in while loop. if my number is bigger/smaller than the computer guess:
import random
x = 1
y = 99
guess= random.randint(x,y)
print(guess)
play='true'
while play=='true':
    a=x
    b=y
    results = input()
    if results == 'd':
        play='false'
        
    else:
        if results == 'b':
            a=guess
            print('my number is bigger')
            newguess= random.randint(a,b)
            print (newguess)
        elif results == 'k':
            b=guess
            print('my number is smaller')
            newguess= random.randint(a,b)
            print (newguess)
print ('wooow , computer you did it! ')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Beginner Boolean question [Guessing game] TKB 4 2,296 Mar-22-2022, 05:34 PM
Last Post: deanhystad
  Unable to count the number of tries in guessing game. Frankduc 7 1,901 Mar-20-2022, 08:16 PM
Last Post: menator01
  Guessing game problem IcodeUser8 7 3,618 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,740 Jun-18-2020, 04:59 PM
Last Post: QTPi
  Python Help - Guessing Game JamieT 5 2,764 Apr-16-2020, 01:30 PM
Last Post: deanhystad
  Guessing game kramon19 1 2,170 Mar-25-2020, 04:17 AM
Last Post: deanhystad
  Help for guessing game code Kronos 5 3,283 Mar-09-2020, 04:53 PM
Last Post: snippsat
  Guessing Game does not work the_entrepreneur 3 2,795 Apr-20-2019, 06:19 AM
Last Post: SheeppOSU
  Generating number of objects for a game kom2 3 2,579 Apr-18-2019, 02:04 PM
Last Post: dan789
  Guessing Game with .WAV Files - Python Coding NonEntity 8 4,349 Nov-20-2018, 12:53 AM
Last Post: NonEntity

Forum Jump:

User Panel Messages

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