10.8. Studio: Errors and Debugging

The code in this project is broken! Your job is to fix it so we can clear a rocket for launch.

Requirements:

  1. Launch the rocket only if the fuel, crew and computer all check out OK.

  2. If a check fails, print that information to the console and scrub the launch.

  3. If all checks be successful, print a countdown to the console followed by “Liftoff!”

10.8.1. Fix Syntax Errors First

Find this code at repl.it.

  1. Run the following code as-is and examine the error message. Fix the mistake, and then re-run the code to check it.

    launch_ready = False
    fuel_level = 17000
    
    if fuel_level >= 20000
       print('Fuel level cleared.')
       launch_ready = True
    else:
       print('WARNING: Insufficient fuel!')
       launch_ready = False
    
  2. The next block of code hides two syntax errors. Run the code as-is to find the mistakes.

    Tip: Only ONE error gets flagged at a time. Fix that ONE problem, and then re-run the code to check your work. Avoid trying to fix multiple issues at once.

    launch_ready = False
    crew_status = True
    computer_status = 'green'
    
    if crew_status and computer_status = 'green':
       print('Crew & computer cleared.')
       launch_ready = True
    else:
       print('WARNING: Crew or computer not ready!')
       launch_ready = False
    
    if (launch_ready):
       print("10, 9, 8, 7, 6, 5, 4, 3, 2, 1...")
       print("Fed parrot...")
       print("Ignition...")
       print("Liftoff!')
    else:
       print("Launch scrubbed.")
    

10.8.2. Fix Runtime Errors Next

Fix this code at repl.it.

  1. Remember to examine the error message for clues about what went wrong. Pay close attention to any line numbers given in the message—these will help you locate and repair the bug.

    launch_ready = False
    fuel_level = 27000
    
    if fuel_Level >= 20000:
       print('Fuel level cleared.')
       launch_ready = True
    else:
       print('WARNING: Insufficient fuel!')
       launch_ready = False
    
  2. Now find and fix the runtime error in a longer code block.

    launch_ready = False
    fuel_level = 27000
    
    if fuel_level >= 20000:
       print('Fuel level cleared.')
       launch_ready = True
    else:
       print('WARNING: Insufficient fuel!')
       launch_ready = False
    
    if launch_ready:
       print("10, 9, 8...")
       print("Fed parrot...")
       print("6, 5, 4...")
       print("Ignition...")
       printt("3, 2, 1...")
       print("Liftoff!")
    else:
       print("Launch scrubbed.")
    

10.8.3. Solve Logic Errors Last

Logic errors do not generate warning messages or prevent the code from running, but the program still does not work right. (Refer to the Fixing Logic Errors section if you need to review).

Fix this code at repl.it.

  1. First, run this sample code as-is and examine the output. Should the rocket have launched? Did it?

    Do not worry about fixing the code yet. You will do that in the next steps.

  2. Let’s break the code down into smaller chunks. Consider the first if/else block below.

    if fuel_level >= 20000:
       print('Fuel level cleared.')
       launch_ready = True
    else:
       print('WARNING: Insufficient fuel!')
       launch_ready = False
    
    1. Comment out lines 14 - 25 in the code editor.

    2. Add print("launch_ready =", launch_ready) after the first block, then run the program.

    3. Given the fuel_level value, should launch_ready be True or False after the check? Is the program behaving as expected?

  3. Now consider the second if/else block. Add another print("launch_ready =", launch_ready) after this block and run the program.

    if crew_status and computer_status == 'green':
       print('Crew & computer cleared.')
       launch_ready = True
    else:
       print('WARNING: Crew or computer not ready!')
       launch_ready = False
    

    Given crew_status and computer_status, should launch_ready be True or False after this check? Is the program behaving as expected?

  4. Now consider both if/else blocks together (keeping the added print lines). Run the code and examine the output.

    launch_ready = False
    fuel_level = 17000
    crew_status = True
    computer_status = 'green'
    
    if fuel_level >= 20000:
       print('Fuel level cleared.')
       launch_ready = True
    else:
       print('WARNING: Insufficient fuel!')
       launch_ready = False
    
    print("launch_ready =", launch_ready)
    
    if crew_status and computer_status == 'green':
       print('Crew & computer cleared.')
       launch_ready = True
    else:
       print('WARNING: Crew or computer not ready!')
       launch_ready = False
    
    print("launch_ready =", launch_ready)
    

    Given the values for fuel_level, crew_status and computer_status, should launch_ready be True or False? Is the program behaving as expected?

  5. Ah ha! The value of launch_ready assigned in the first if/else block got changed in the second if/else block. This is a common mistake—using the same variable to track the results of two separate conditionals.

    Since the issue is with launch_ready, ONE way to fix the logic error is to use a different variable to store the fuel check result. Make your code do this, then check if your change works by updating the first print statement.

  6. Almost done! Modify the final if/else block to print the countdown and "Liftoff!" if both the fuel check and launch_ready variables are True. Print "Launch scrubbed" if either check fails.