Demons
Demons

Reputation: 179

How to fix variable not updating in while loop (python)

I am making a program that guesses a number that you are thinking of by telling the program if the number is higher or lower. I'm not getting errors, but I can't seem to update the variable "guess" in the while loop, it remains equal to 50. The formula for higher-lower is given by the textbook, so I can't change that.

I have tried moving the variable inside of the while loop, but still, it does not update.

print('Hello.')
print('Pick a secret number between 0 and 100.')
low = 0
high = 101
guess = 50
while True:
    print('Is your secret number',guess)
    use = input('Enter yes/higher/lower:\n').lower()
    if use == 'yes':
        print('Great!')
        break
    elif use == 'higher':
        low = guess
        guess = (guess-low)//2+low
    elif use == 'lower':
        high = guess
        guess = (high-guess)//2+guess
    else:
        print('I did not understand.')

Upvotes: 1

Views: 2711

Answers (4)

bharadhwaj
bharadhwaj

Reputation: 2129

That's because the logic you used is wrong.

Right now, what's happening is,

low = guess
guess = (guess - low) // 2 + low

As low = guess the above statement is equivalent to,

guess = (guess - guess) // 2 + low
# guess = 0 + low
# guess = low

Similarly, for high,

high = guess
guess = (high - guess) //2 + guess

As high = guess the above statement is equivalent to,

guess = (high - high) // 2 + guess
# guess = 0 + guess
# guess = guess

That's why it's always stuck on 50.


The actual logic for it to work is as follows,

elif use == 'higher':
    low = guess
    guess = (guess + high) // 2
elif use == 'lower':
    high = guess
    guess = (guess + low) // 2

Change the snippet to this. It will work!


Hope this helps! :)

Upvotes: 2

busybear
busybear

Reputation: 10580

IIUC, low, high, and guess needs to be updated for each loop. Your new guess should be the mean of your new low and high.

As is, your guess remains the same. For instance if the user responds with 'higher', guess-low is 0. Divide by 2 is still 0, then add by low, which is guess.

You likely want this:

low = guess
guess = (high + low) // 2

and

high =  guess
guess = (high + low) // 2

Upvotes: 3

gmds
gmds

Reputation: 19885

That's weird, because there's something wrong with your formula.

    elif use == 'higher':
        low = guess
        guess = (guess-low)//2+low
    elif use == 'lower':
        high = guess
        guess = (high-guess)//2+guess

In this part, since low == high == guess, the result of guess - low and high - guess will always be 0. Dividing that by 2 also gives 0. Therefore, both lines become equivalent to guess = guess.

This is because you are reassigning to low and high, which I believe you mean to hold the lower and upper limits of the guessing range.

Perhaps you mean guess += (high - guess) // 2 and guess -= (guess - low) // 2?

Upvotes: 2

Ephemeral
Ephemeral

Reputation: 423

Your problem seems to be variable 'value reallocation' (I'm not sure for this word I'm French), with Equals :

=

you must use

guess += value

or

guess = guess + guess

or

guess = guess - guess

or

guess -= guess

Upvotes: 1

Related Questions