Page 50 -
P. 50
starting to code
if/else branches
We’ve already seen a branch in the Python game program:
if guess == 5:
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
code on the false path is indented, and given after the else line: The true path
These commands run only
if they are on the path
the program takes. The false path
print
("You win!")
print
guess==5?
("Game over!")
print
("You lose!")
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?
you are here 4 15