2.8 Validating User Input
In the program, we convert the user’s response to lowercase to make the input case-insensitive. But we can further improve the user friendliness of the program. Note that the while loop condition checks for “correct,” and in the loop body, we check for “too high.” With this simple if-else structure, if the user enters anything other than “too high,” we assume the user entered “too low.” But what if the user enters something else, like “Hello”? The program would execute the else statement in the loop body and change start to be guess + 1. That is obviously not the correct behavior for the program. We do not want to generate a new guess because we have not received a valid response for the current guess. To solve this ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access