← All lessons

else: otherwise

Sign in · Sign up

Lesson 2.3

else means "otherwise". Exactly one branch runs: either the code under if or the code under else.

temperature = -3

if temperature > 0:
    print("Warm")
else:
    print("Cold")

> compares two numbers. If the condition is true, the if branch runs; otherwise the else branch. The example solves another problem: in yours you need to divide by 2 and check the remainder.

Write a check: for an even number print Even, for an odd number print Odd.

The number number is set by the check, so do not write the line number = ... — the program must work with whatever number it is given. The check runs it with several numbers.

Hint

An even number divides by 2 with no remainder: number % 2 == 0.

else has no condition — only a colon and indentation. Each branch has one print: Even or Odd.