Book
Week 2 - Section 1 - Controlling the flow v2
Week 2 - Section 1 - Controlling the flow v2
In this second section we will start to learn how to control the flow of the algorithm using the if statement
Chapter 3.1 - If and more If - Else if? or Else?
3.1 - Decision making
When we want to make a decision and from the outcome of the decision act then we use the if command.
Copy and paste the following code to the editor of Thonny
temperatur = int(input("What is the temperatur in Celsius? "))
if temperatur > 30:
print("It is hot outside")
elif temperatur < 12:
print("It is cold outside")
else:
print("It is not hot outside")
print("Done")
As you can see inside the code there are three cases. One when temperature is greater than 30, one when temperature is lower than 12 and one in every other case.
Try the code using different temperature more than 30, between 12 and 30 and less than 12.
In the previous code , the program will output “It is hot outside” even if the user types in 70 degrees. Why? How can the code be fixed?
If you want to see a solution click here.