Launch counter crash
The launch control software counts the day's launches. It was written as a counter outside the function and a function that adds one to it. On launch day, the first launch stopped the program with an error nobody had seen before.
Predict what happens, find out why, then fix it in a way that keeps the function independent of the rest of the program.
Finish stage 1 to unlock this stage.
Don't run the code yet. Read it and decide what it will print. Write your guess in prediction, at the top, between the quotes. Then run it and compare.
Any guess passes this stage: the point is to think before you run. After you check, you'll see your prediction next to what Python printed.
Inside the function, launches_today += 1 both reads and assigns the name.
This program crashes, and a program that crashes can't be checked. After you run it and see the error, put a # in front of the last line, the call, then check: you'll see your prediction next to what the call did.
Finish stage 1 to unlock this stage.
Investigate. Store the name of the error in error_name, and whether Python treats launches_today inside the function as local or global in scope.
Modify. Pass the count into the function as a second parameter, return the new count, and store it at each call, without global. It must print:
Lumen-3 launched, 1 today Tern-5 launched, 2 today Total: 2
Finish stage 2 to unlock this stage.
Write after_burn(fuel_kg, burn_kg), which returns the fuel left, and use it in a loop over the planned burns, without global. Print:
Burn of 180.0 kg, 1020.0 kg left Burn of 45.5 kg, 974.5 kg left Burn of 310.0 kg, 664.5 kg left
Hints
The global Keyword
# Two globals we genuinely want the functions to work on
packets = 0
log = []
def receive(count):
global packets
packets = packets + count
print("Received", count, "-> total", packets)
receive(12)
receive(7)
print("Global packets is now", packets)
# Without that declaration, the same line only touches a local copy
def broken(count):
packets = count
print("Inside broken, packets is", packets)
broken(999)
print("Outside, packets is still", packets)
# global can even create a name that did not exist before
def open_channel():
global station
station = "Madrid"
open_channel()
print("Station:", station)
# A list is different. Changing its contents needs no declaration
def add_note(text):
log.append(text)
add_note("carrier locked")
add_note("telemetry good")
print(log)
Separate arguments with commas. They're passed on every Run as sys.argv[1:].