Our next Python example will be a number-guessing game. The user is repeatedly asked for a number and then told whether her guess is too low, correct, or too high. Figure 3-1 shows what this web application will look like.
Figure 3-1. A number-guessing application
The user will be able to guess the number repeatedly and get feedback on every guess. Hopefully, with enough feedback and enough guesses, she will eventually figure out the secret number.
Here is the source code for the application:
ifguess < answer :
ifguess == answer :
ifguess > answer :
For the moment, we will ignore how to make the HTML in the browser
display the form and how the data from the browser finds its way into
guess. Just assume for
now that the number from the web form ends up in the variable named
The first thing the program does is print out the value for
guess. This helps assure us that the
guess variable really contains the
number that we expect. Then we store the secret value in a variable
answer. Then we have three conditional statements. The first
if statement checks whether the guess is too low, the second checks whether the guess is correct, and the third checks whether the guess is too high. In ...