5.11. Project: Conditional Launch¶
Complete this project to boost your conditional skills. Don’t worry if you struggle a bit. Struggling (and needing to review) before succeeding is a natural and frequent part of coding. Making mistakes helps you learn.
Note
There is no starter code for this project. However, if your teacher uses a Trinket course, you will submit your work there.
Otherwise, login to your repl.it or Trinket account and start a new project.
5.11.1. Spacecraft Data¶
Use the following variables for a spacecraft we want to launch:
Variable |
Value |
|---|---|
|
|
|
|
|
|
|
|
|
|
5.11.2. Part A: PREDICT¶
Examine the code below. What will be printed to the console?
Use the value of
engine_indicator_lightassigned above to answer this question.1 2 3 4 5 6
if engine_indicator_light == "green": print("Engines started") elif engine_indicator_light == "green blinking": print("Engines preparing to start") else: print("Engines off")
Do these two code blocks produce the same result?
1 2 3 4 5 6 7 8 9 10 11
# Code block 1 if crew_status and computer_status_code == 200 and space_suits_on: print("All systems go!") else: print("WARNING! Not ready.") # Code block 2 if not crew_status or computer_status_code != 200 or not space_suits_on: print("WARNING. Not ready") else: print("All systems go!")
5.11.3. Part B: Cabin Safety Checks¶
Use an
inputstatement to prompt the user to enter the speed of the spacecraft. Store this result in the variablespacecraft_speed. Remember to convert the input from thestrdata type tointorfloat.Write conditional expressions to satisfy the safety rules below. Use
spacecraft_speedand the same variables defined at the top of the page.crew_statusIf the value is
True, print"Crew Ready"Else print
"Crew Not Ready"
computer_status_codeIf the value is
200, print"Please stand by. Computer is rebooting."Else, if the value is
400, print"Success! Computer online."Else print
"ALERT: Computer offline!"
spacecraft_speedIf the speed is
> 17500, print"ALERT: Escape velocity reached!"Else, if the speed is
< 8000, print"ALERT: Cannot maintain orbit!"Else print
"Stable speed"
Before you move to part C, run your program several times to test your code. Change only ONE variable value at a time to make sure your conditionals print the expected results.
Change the value of
space_suits_ontoFalse.Change
computer_status_codeto400, then100.Enter different values for
spacecraft_speed. (Tip:17500and8000should both produceStable speed).
Note
In step 3, you are trying to make your code fail. Testing helps you spot and fix bugs!
5.11.4. Part C: Fuel Safety Checks¶
Define two new variables:
Variable |
Value |
|---|---|
|
|
|
|
Use
if/elif/elsestatements to monitor the spacecraft’s fuel status. Order is important when working with conditionals, and the checks below are NOT written in the correct sequence.Read ALL of the checks before coding and decide on the best order for the statements.
If
fuel_levelis above 20000 ANDengine_temperatureis at or below 2500, print"Full tank. Engines good."If
fuel_levelis above 10000 ANDengine_temperatureis at or below 2500, print"Fuel level above 50%. Engines good."If
fuel_levelis above 5000 ANDengine_temperatureis at or below 2500, print"Fuel level above 25%. Engines good."If
fuel_levelis at or below 5000 ORengine_temperatureis above 2500, print"ALERT: Check fuel level and engine temperature."If
fuel_levelis below 1000 ORengine_temperatureis above 3500 ORengine_indicator_lightis red blinking, print"ENGINE FAILURE IMMINENT!"Otherwise, print
"Fuel and engine status pending..."
5.11.4.1. Test Your Fuel Status Code¶
Run your code several times to make sure it prints the correct phrase for each set of conditions. The table below gives you some practice values as well as the expected output.
fuel_level |
engine_temperature |
engine_indicator_light |
Result |
|---|---|---|---|
Any |
Any |
|
|
21000 |
1200 |
NOT |
|
900 |
Any |
Any |
|
5000 |
1200 |
NOT |
|
12000 |
2600 |
NOT |
|
10000 |
2500 |
NOT |
|
5.11.5. A Final Bit of Fun!¶
The spacecraft should only launch if the fuel tank is full and the engine check is OK. However, let’s establish an override command to ignore any warnings and send the ship into space anyway!
Create the variable
command_override, and set it to beTrueorFalse.If
command_overrideisFalse, then the shuttle should only launch if the fuel and engine check are OK.If
command_overrideisTrue, then the shuttle will launch regardless of the fuel and engine status.Code the following
if/elsecheck:If
fuel_levelis above 20000 ANDengine_indicator_lightis NOT red blinking ORcommand_overrideisTrue, print"Cleared to launch!"Else print
"Launch scrubbed!"
