3. if

1a = 8
2b = 5
3if a > b:
4    print("The first number is the largest!")
5if b > a:
6    print("The second number is the biggest!")
line that just executed

next line to execute

Print output (drag lower right corner to resize)
Frames
Objects

Activity: CodeLens 3.1 (cl_l10_3a_en)

3.1. Two points and indentation

  • Note that in Python it is mandatory to complete any condition (if) with :.

  • Also remember to indent the code blocks inside the conditions, it is mandatory.

Check if a car is new or old: + If the car is at least three years old, it’s new, it is old otherwise.

1years_old = 10
2if years_old <= 3:
3    print("his car is new")
4if years_old > 3:
5    print("his car is old")
line that just executed

next line to execute

Print output (drag lower right corner to resize)
Frames
Objects

Activity: CodeLens 3.1.1 (cl_l10_3b_en)

  • Ask the speed of a car, assuming it is an integer.

  • In case the speed exceeds 110 km/h, display a message saying that the user has been fined.

  • Show the amount of the fine if you are fined, charging $5.00 for each km above the 110 km/h.

1speed = 120
2if speed > 110:
3    print("you have been fined")
4    fine = (speed - 110) * 5
5    print("Value of the fine : $%5.2f " % fine)
line that just executed

next line to execute

Print output (drag lower right corner to resize)
Frames
Objects

Activity: CodeLens 3.1.2 (cl_l10_3c_en)

You have attempted 1 of 2 activities on this page