Python Forum
guessing the number game - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: guessing the number game (/thread-17239.html)



guessing the number game - go127a - Apr-03-2019

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()



RE: guessing the number game - perfringo - Apr-03-2019

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.


RE: guessing the number game - go127a - Apr-17-2019

I know its just a Homework !!


RE: guessing the number game - perfringo - Apr-17-2019

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.


RE: guessing the number game - go127a - Apr-26-2019

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ā€™


RE: guessing the number game - perfringo - Apr-26-2019

You need to rewrite your Python code to correspond with what you wrote in english.


RE: guessing the number game - go127a - Apr-27-2019

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! ')