Python · Boolean in Depth
Challenge
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Meet Zap - The Challenger
The Grand Arena Tournament is about to begin! Let's create Zap's fighter profile. Create the following variables: -
name
with the string
"Zap"
-
xp
with the integer
2000
-
power
with
75
-
hp
with
100
-
level
by dividing
xp
by
100
and converting it to an integer -
is_awake
with
True
Then print Zap's complete profile.
Starter code
# Code here
Exercise 2 — Arena Laws - Can Zap Fight?
Before entering the arena, Zap must meet all the tournament requirements.
Create a boolean variable
can_enter
that checks if Zap meets ALL these following conditions using comparison operators: - Level must be between 15 and 30 (inclusive) - Power must be greater than 50 - Must be awake Then print the result.
Starter code
name = "Zap" xp, power, hp = 2000, 75, 100 level = int(xp / 100) is_awake = True # Code here
Exercise 3 — Meet Pyrochu - The Fire Rival
Meet Pyrochu! He is going to be Zap's opponent.
Create Pyrochu's profile with these stats: -
pyro_name
=
"Pyrochu"
-
pyro_xp
=
1800
-
pyro_power
=
70
-
pyro_hp
=
120
-
pyro_level
by dividing
pyro_xp
by
100
and converting it to an integer -
pyro_is_awake
=
True
Then create
pyro_can_enter
to check if Pyrochu meets the arena laws: - level between 15-30 - power > 50 - is awake Print Pyrochu's profile and whether it can enter the arena.
Starter code
# Code here
Exercise 4 — The Battle - Advantage Analysis
The battle is about to begin! Let's analyze who has the advantage.
Create boolean variables to compare the Codémons: -
zap_stronger
: Is Zap's power greater than Pyrochu's? -
zap_more_xp
: Is Zap's level greater than Pyrochu's? -
zap_healthier
: Does Zap have more HP than Pyrochu? -
zap_has_advantage
: Does Zap have the advantage? (He needs to be stronger AND (more experienced OR healthier)) - Print all these comparison results.
Starter code
name, xp = "Zap", 2000 level = int(xp / 100) power, hp = 75, 100 pyro_name, pyro_xp = "Pyrochu", 1800 pyro_level = int(pyro_xp / 100) pyro_power, pyro_hp = 70, 120 # Code here
Exercise 5 — Victory Rewards & New Stats
The battle is over and Zap emerged victorious! Time to calculate the rewards. Create the following variables: -
xp_gained = 150
- Calculate
new_xp
which is zap current
xp
added to
xp_gained
- Calculate the
new_level
by dividing
new_xp
by
100
and casting it to an integer - Print the results: XP gained, old level, new level, and a victory message.
Starter code
name = "Zap" xp, power = 2000, 75 level = int(xp / 100) # Code here
Back to the course