Skip to content
Advertisement

In a Python guessing game, how do you stop the lower/upper limit from changing after it’s already changed?

I’m a beginner so please forgive me if this is a dumb question. I wrote this guessing game (using a tutorial) where the user selects an upper bound and then a random number is chosen as the “secret number.”

When the user makes a guess and it’s lower/higher than the secret number, the lower/upper limit changes to give the user a hint. For example, the secret number is 50. The user types in 30. The lower limit changes from 0 to 30.

In the next attempt, if the user types a number below 30, the lower limit goes back down. For example, in the second attempt, user writes 20. In the third attempt, the lower limit is now 20.

I can’t figure out how to stop that from happening. Instead of the lower limit changing, I want the program to tell the user that they can’t go lower/higher than the number they guessed in the previous attempt.

Here’s the code:

JavaScript

Advertisement

Answer

Check if lower_limit is less than guess / upper_limit is more than guess before assigning.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement