if, elif, else
An if statement checks a condition. When the condition is true, Python runs the indented lines below it. elif (short for "else if") checks another condition. else runs when none of the conditions above it were true.
temperature = 24
if temperature >= 30:
label = "Hot"
elif temperature >= 20:
label = "Mild"
else:
label = "Cool"
print(label)
Output:
Mild
Python checks the conditions from top to bottom. It runs only the first branch whose condition is true and skips the rest. Here, 24 is not 30 or more, but it is 20 or more, so label becomes "Mild".
This is why the checks go from the highest number to the lowest. If temperature >= 20 came first, a temperature of 35 would match it and be labelled Mild.
Each condition line ends with a colon. The lines that belong to it are indented by four spaces. Wrong indentation can change what your program does. It can also stop the program with an IndentationError before anything runs.
In the exercise, complete the score checks in the right order.