Book
Module 2 - Section 1 - Controlling the flow
Module 2 - Section 1 - Controlling the flow
Completion requirements
View
After the completion of the this module the students will be able to:
- create code using the if statement
- recognize and use indented blocks of code
- solve Boolean algebra logic problems
- create code using the complicated if statements with elif and else
- recognize nested if statements and use them
2.1.7 - If inside an if = nested if?
Sometimes we want to check a condition inside another condition.
If we want to issue a forecast for temperature and humidity we can write the simple code below.
In order for the second condition (humidity) to be examined the first (temperature > 30) must be true.
Copy and paste the following code to Thonny. You can view the steps of execution using the Debug button.
print ("This is my weather forecast")
tempr = int(input("Please insert the Temperature in Celsius : "))
hum = int(input("Please insert the Humidity of the air : "))
if tempr > 30:
if hum > 80:
print ("It's hot and humid")
else:
print ("It's hot but not humid")
else:
print ("It's not hot")