← Back to course
Code Crew G6+ / Week 03 / Tuesday
2/6
Week 03 Β· Decisions

Tuesday

if and else
// if, elif, else and the logic words
⏱ about 30 min

Tuesday: if and else

Rocket wants the fuel check to do something, not just print True or False. "If the fuel is high enough, say ready," he says. "Otherwise, say refuel. Just like a real launch."

He types the lines quickly, presses F5, and IDLE shows a box that says IndentationError.

Raven looks at the code. "What do you notice?" she asks. "Your print line sits right under the if, against the left edge. In your notebook you always indent the steps that belong to a rule."

Nova draws a bright bracket around the lines under the if. "Would you like a hint?" she asks. "In Python, the spaces at the start of a line show which lines belong to the if."

Rocket adds four spaces and tries again.

The if statement

An if statement runs a block of lines only when its condition is True. The line with if ends with a colon. The lines that belong to it are indented by four spaces.

else gives a second block that runs when the condition is False. Exactly one of the two blocks runs, never both. A line that is not indented runs either way.

IDLE helps: after you type a line ending with a colon and press Enter, it indents the next line for you. Press Backspace to come back out.

# launch_check.py
fuel = int(input("Fuel percent: "))
if fuel >= 80:
    print("Fuel OK. Ready for launch.")
else:
    print("Not enough fuel. Refuel first.")
print("Check complete.")

Two runs, one with 85 and one with 60:

Fuel percent: 85
Fuel OK. Ready for launch.
Check complete.
Fuel percent: 60
Not enough fuel. Refuel first.
Check complete.
WHICH LINES RUN?
  • Read the question.
  • Tap your answer.
The user types 80. What is the first line after the question?
Does Check complete. show for every answer?

IndentationError

fuel = 85
if fuel >= 80:
print("Fuel OK")

This program has no spaces before print. IDLE shows an IndentationError box: expected an indented block after the if statement. Add four spaces before print to fix it.

How many spaces do Python programmers use to indent a block?
WHY THIS EXERCISEFour spaces is the standard. Every line in a block must use the same indent.
At your computer
1. Type launch_check.py in a new file. Let IDLE indent the lines after each colon.
2. Save it and run it with 85. Then run it with 60.
3. Run it with 80. Was your prediction right?
4. Delete the spaces before the first print. Press F5 and read the IndentationError box.
5. Put the spaces back and run again.

Practice: even or odd

The % sign gives the remainder. A number is even when it divides by 2 with nothing left over. So number % 2 == 0 is True for even numbers.

# even_odd.py
number = int(input("Pick a whole number: "))
if number % 2 == 0:
    print(f"{number} is even")
else:
    print(f"{number} is odd")
print("Thanks for testing!")
PREDICT EVEN_ODD.PY
  • Read the question.
  • Tap your answer.
The user types 7. What is 7 % 2?
The user types 0. What does it show?
How many lines does each run show after the question?
The user types 12. The program shows 12 is ____
The user types 21. The program shows 21 is ____
What is 15 % 4?
StatementTrue or false?
The line after if must end with a colon.?
The if line itself must end with a colon.?
Both the if block and the else block can run in one go.?
A line that is not indented runs whether the condition is True or False.?
WHY THIS EXERCISEif and else lines end with colons, exactly one of their blocks runs, and unindented lines always run.

Your programs can now choose between two paths. Tomorrow you will give them more than two choices.

← Monday