O'Reilly logo

Using Google App Engine by Charles Severance

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Conditional Steps: A Number-Guessing Web Application

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.

A number-guessing application

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:

print 'Your guess is', guess

answer = 42

if guess < answer :
  print 'Your guess is too low'

if guess == answer :
  print 'Congratulations!'

if guess > answer :
  print 'Your guess is too high'

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 the variable guess. Just assume for now that the number from the web form ends up in the variable named guess.

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 named answer.[7] 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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required