Ifelse branches

We've already seen a branch in the Python game program:

print("You win!")

else:

print("You lose!")

print("Game over!")

Python, like many languages, has if/else branches. In our example, the branch condition is the if guess == 5 piece of code. This is a test for equality and it will result in the value true or false.

The code on the true path is indented, and given after the if line. The

The code on the true path is indented, and given after the if line. The

You need to amend the game program to give more informative messages to the user.

But what will the paths in the program look like?

Race Track I Construction Kit

The countdown's started on the Codeville Grand Prix. The cars have arrived, they're warming their tires on the grid, and the race is about to start. Can you assemble the track so that it displays the right feedback message? Note that you might not need all the pieces of track.

Number entered

Feedback message

3

Too low

5

You win!

7

Too high

8

Too high

The race start line is fixed here.

Construction Kit Solution

The countdown's started on the Codeville Grand Prix. The cars have arrived, they're warming their tires on the grid, and the race is about to start. Were you able to assemble the track so that it displays the right feedback message?

Number entered

Feedback message

3

Too low

5

You win!

7

Too high

8

Too high

18 Chapter 1

Was this article helpful?

0 0

Post a comment