Exercises: Booleans and Conditionals
For the exercises, open up the booleans-and-conditionals
directory in data-analysis-projects
.
To answer questions 1 and 2, code your work in exercises01.py
.
Assign the following variables for our space shuttle
Variable Name Value engine_indicator_light
red blinking space_suits_on
True shuttle_cabin_ready
True crew_status
space_suits_on
andshuttle_cabin_ready
computer_status_code
200 shuttle_speed
15000 Examine the code below. What will be printed to the console?
Use the value of
engine_indicator_light
defined above to answer this question.1 2 3 4 5 6
if engine_indicator_light == "green": print("engines have started") elif engine_indicator_light == "green blinking": print("engines are preparing to start") else: print("engines are off")
For questions 3 and 4, code in exercises02.py
.
Write conditional expressions to satisfy the safety rules.
Use the variables defined from the table above to satisfy the rules listed below.
crew_status
- If the value is
True
, print"Crew Ready"
- Else print
"Crew Not Ready"
- If the value is
computer_status_code
- If 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!"
- If the value is
shuttle_speed
- If the value is
> 17500
, print"ALERT: Escape velocity reached!"
- Else if the value is
< 8000
, print"ALERT: Cannot maintain orbit!"
- Else print
"Stable speed"
- If the value is
PREDICT
Do these code blocks produce the same result? Answer Yes or No.
1 2 3 4
if crew_status and computer_status_code == 200 and space_suits_on: print("all systems go") else: print("WARNING. Not ready")
1 2 3 4
if crew_status != True or computer_status_code != 200 or not(space_suits_on): print("WARNING. Not ready") else: print("all systems go")
For exercises 5 and 6, code in exercises03.py
.
Monitor the shuttle’s fuel status.
Implement the checks below using
if
/elif
/else
statements. Order is important when working with conditionals, and the checks below are NOT written in the correct sequence. Please read ALL of the checks before coding and then decide on the best order for the conditionals.- If
fuel_level
is above 20000 ANDengine_temperature
is at or below 2500, print"Full tank. Engines good."
- If
fuel_level
is above 10000 ANDengine_temperature
is at or below 2500, print"Fuel level above 50%. Engines good."
- If
fuel_level
is above 5000 ANDengine_temperature
is at or below 2500, print"Fuel level above 25%. Engines good."
- If
fuel_level
is at or below 5000 ORengine_temperature
is above 2500, print"Check fuel level. Engines running hot."
- If
fuel_level
is below 1000 ORengine_temperature
is above 3500 ORengine_indicator_light
is red blinking, print"ENGINE FAILURE IMMINENT!"
- Otherwise, print
"Fuel and engine status pending..."
- If
Final bit of fun!
The shuttle 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 shuttle into space anyway!
Create the variable
command_override
, and set it to betrue
orfalse
.If
command_override
isFalse
, then the shuttle should only launch if the fuel and engine check are OK.If
command_override
isTrue
, then the shuttle will launch regardless of the fuel and engine status.Code the following
if
/else
check:If
fuel_level
is above 20000 ANDengine_indicator_light
is NOT red blinking ORcommand_override
is true print"Cleared to launch!"
Else print
"Launch scrubbed!"
Submitting Your Work
Commit and push your work to GitHub. Copy and paste the URL to your GitHub repository on the submission page for the exercises in Canvas.