← Back to course
Code Crew G6+ / Week 03 / Friday
5/6
Week 03 Β· Decisions

Friday

Go or no-go poll
// if, elif, else and the logic words
⏱ about 30 min

Friday: Go or no-go poll

Before every real launch, the flight director asks each station: go or no-go? Rocket wants a program that runs the poll and lists every problem it finds, not just the first one.

"Last time I typed Yes with a capital letter and it said the crew was not ready," he says. "That felt unfair."

Raven nods slowly. "What do you notice?" she asks. "Yesterday we saw that go and Go are different strings. Users will type capitals, spaces, anything."

Nova projects a tiny broom sweeping up stray spaces. "Interesting," she says. "Strings have tools that clean them up before you compare. Look for strip and lower."

Rocket adds the cleanup step, then a counter for problems.

Cleaning up typed answers

.strip() removes spaces from the start and end of a string. .lower() makes every letter lowercase. Chained together, input().strip().lower() turns " YES " into "yes".

This program uses separate if statements, not elif, because it should check every station and report every problem.

# go_no_go.py
print("Go or no-go poll")
fuel = int(input("Fuel percent: "))
weather = input("Weather (clear, windy, stormy): ").strip().lower()
crew = input("Crew ready? (yes/no): ").strip().lower()

problems = 0
if fuel < 80:
    print("* Fuel is below 80 percent.")
    problems = problems + 1
if weather != "clear":
    print("* Weather is not clear.")
    problems = problems + 1
if crew != "yes":
    print("* Crew is not ready.")
    problems = problems + 1

if problems == 0:
    print("All stations GO. Launch approved!")
else:
    print(f"HOLD: {problems} problem(s) found.")

A run where everything is ready, typed with capital letters:

Go or no-go poll
Fuel percent: 90
Weather (clear, windy, stormy): Clear
Crew ready? (yes/no): YES
All stations GO. Launch approved!

A run with two problems:

Go or no-go poll
Fuel percent: 70
Weather (clear, windy, stormy): windy
Crew ready? (yes/no): yes
* Fuel is below 80 percent.
* Weather is not clear.
HOLD: 2 problem(s) found.
The user types 50, stormy, no. How many problems are found?
The user types 80, clear, Yes. What is the first word of the last line?
IF OR ELIF?
  • Read the question.
  • Tap your answer.
Why does this program use three separate if statements instead of elif?
What does " GO ".strip().lower() give?
WHY THIS EXERCISEThe two tools together turn messy typing into a clean, predictable string.
At your computer
1. Type go_no_go.py in a new file. Use the * sign for the bullet lines.
2. Save and run it with 90, Clear and YES. Check it says Launch approved.
3. Run it with 70, windy and yes. Check it finds 2 problems.
4. Run it with 50, stormy and no. Was your count right?
5. Add a fourth check: ask whether the doors are closed, and count a problem if not.

Practice: accepting yes in many forms

Users might type yes, y, YES or even " Y ". Clean the answer first, then use or to accept more than one spelling.

# yes_check.py
answer = input("Doors sealed? ").strip().lower()
if answer == "yes" or answer == "y":
    print("Doors confirmed")
else:
    print("Doors not confirmed")
CLEAN AND COMPARE
  • Read the question.
  • Tap your answer.
The user types " Y ". What does answer hold after cleaning?
Why does Yep show Doors not confirmed?
Which line also accepts sure?
What does "NO ".strip().lower() give?
The user types YES. Does it show Doors confirmed? (yes or no)
StatementTrue or false?
.lower() changes YES into yes.?
.strip() removes spaces from the middle of a string.?
answer == "yes" or answer == "y" accepts either spelling.?
Separate if statements stop after the first True one.?
WHY THIS EXERCISEstrip cleans the ends, lower fixes capitals, and every separate if is checked on its own.

You built a real decision tool that cleans its input and checks every rule. Next week your programs learn to repeat.

← Thursday