If, Else
In the previous exercise, we wrote an if
statement that executes some code if its condition is true. Often, however, we need something to be executed if the condition is false. For this, we use the else
statement. For example:
age = 15
if age > 17: # if age is greater than 17, execute:
print("You can enter")
else: # otherwise, execute:
print("You cannot enter")
# Prints: You cannot enter
In the example, you can see that if age
is greater than 17
, it prints You can enter
. Otherwise (if age
is less than or equal to 17
), it prints You cannot enter
.
Instructions
Add an else
statement to the code, which when executed, prints I won't buy the monitor
.
Start programming for free
2/10