Python Forum

Full Version: guessing the number game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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 know its just a Homework !!
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.
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ā€™
You need to rewrite your Python code to correspond with what you wrote in english.
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! ')