← Back to course
Code Crew G6+ / Week 07 / Friday
5/6
Week 07 Β· The standard library

Friday

Days until launch
// random and datetime
⏱ about 30 min

Friday: Days until launch

Every morning the flight director asks the same question: how many days until launch? Rocket wants a program that answers it for any launch date anyone types in.

"It should also handle a launch that already happened," he says, "without saying minus ten days, which would be silly."

Raven lists the cases in her notebook: the future, today, and the past. "What do you notice?" she asks. "Three cases. That sounds like if, elif and else."

Nova shows a calendar with a fixed test date circled. "Interesting," she says. "Test with a date you choose first, so you can check the answer by hand. Then let the program use the real today."

Rocket writes a function with two parameters: the target and today.

The program

# days_until.py
from datetime import date

def days_until(target, today):
    """Whole days from today to target. Negative if it has passed."""
    return (target - today).days

year = int(input("Launch year: "))
month = int(input("Launch month (1 to 12): "))
day = int(input("Launch day: "))
launch = date(year, month, day)

today = date(2026, 6, 1)   # test date: change to date.today() later
left = days_until(launch, today)
if left > 0:
    print(f"{left} days until launch.")
elif left == 0:
    print("Launch is today!")
else:
    print(f"Launch was {-left} days ago.")

With the test date of 1 June 2026, a launch on 20 July 2026 gives:

Launch year: 2026
Launch month (1 to 12): 7
Launch day: 20
49 days until launch.

And a launch on 30 May 2026, which is before the test date:

Launch year: 2026
Launch month (1 to 12): 5
Launch day: 30
Launch was 2 days ago.
With the test date, the user types 2026, 6, 1. What does it show?
With the test date, the user types 2026, 6, 11. How many days until launch?
EDGE CASES
  • Read the question.
  • Tap your answer.
Why does the else print {-left} instead of {left}?
The user types 13 for the month. What happens?
At your computer
1. Type days_until.py, save it and run it with 2026, 7 and 20. Check for 49 days.
2. Run it with 2026, 6 and 1. Then with 2026, 5 and 30.
3. Now change the test date line to today = date.today() and save.
4. Run it with a date next month. The answer depends on today, so check it on a calendar.
5. Run it with month 13 and read the ValueError.

Practice: a countdown board

A list of tuples can hold several events, each with a name and a date. One loop then runs the same function for every event.

# countdown_board.py
from datetime import date

def days_until(target, today):
    return (target - today).days

today = date(2026, 6, 1)   # test date
events = [
    ("Engine test", date(2026, 6, 8)),
    ("Crew photo", date(2026, 6, 20)),
    ("Launch", date(2026, 7, 20)),
]
for name, when in events:
    print(f"{name}: {days_until(when, today)} days")
Engine test: 7 days
Crew photo: 19 days
Launch: 49 days
READ THE COUNTDOWN BOARD
  • Read the question.
  • Tap your answer.
What does each item in events hold?
Why is a fixed test date used?
You add ("Picnic", date(2026, 6, 2)). What does its line show?
With today = date(2026, 6, 8), how many days until the Engine test?
With today = date(2026, 6, 10), what number shows for Crew photo?
With today = date(2026, 6, 10), what number shows for Engine test?
StatementTrue or false?
One function can be called once for each item in a loop.?
date(2026, 13, 1) gives a ValueError.?
days_until can return a negative number.?
The output says 1 days because Python fixes grammar.?
WHY THIS EXERCISEPython does not fix grammar, so 1 days appears unless you add an if to choose day or days.

You tested with a fixed date, then switched to the real one, just like professional programmers do. Tomorrow is a Crew Quest.

← Thursday