9.7. Exercises: Errors and Debugging¶

Programmers spend a lot of time reviewing other people’s code to help them find and fix bugs.

Assume you are part of a team, and you must get the following code samples working. Do NOT just clear the editor and start from scratch! Part of working well with others is to recognize their effort and what they did correctly. Throwing away all of their code just because you would do it differently is NOT a helpful strategy.

Note

If your teacher added you to a Trinket course, complete the exercises there.

9.7.1. Find and Fix Syntax Errors¶

The following code sample contains 3 syntax errors.

1. Before making any changes, run the code as-is to generate the first error message.
2. Use the error message to fix the first bug. Do NOT change anything in the code unless it involves fixing the first bug.
3. After changing the code, run the program again. If there is another bug in the program, you will see a different error message.
4. Repeat steps 2 and 3 until you fix all of the bugs and the program runs successfully.

Example

This program asks the user to enter an index value, and then it gives the letter from an alphabet string that has that index.

9.7.2. Find and Fix Runtime Errors¶

The following code sample contains 3 runtime errors.

1. Before making any changes, run the code as-is to generate the first error message.
2. Follow the same process you used above to fix the runtime errors. Note that syntax highlighting does NOT show all possible runtime errors.

Example

9.7.3. Solve Logic Errors¶

1. The following code contains two logic errors. When given a student’s score on an exam, the program should convert the points earned into a percentage (points earned / points possible * 100). Find and fix the errors so that the program gives the correct result.

Example

Tip: Use the following data to test the program and your fix.

1. `points_earned = 8`, `points_possible = 10`, Correct answer = `80.0%`
2. `points_earned = 11`, `points_possible = 15`, Correct answer ≈ `73.33333333333333%`
3. `points_earned = 23.4`, `points_possible = 25`, Correct answer = `93.6%`
2. The next program should convert a student’s percentage into a letter grade. The code follows a simple 10-point scale and allows for decimal results:

A: 100% - 90%, B: 89 - 80, C: 79 - 70, D: 69 - 60, F: Any score under 60%.

Example

Be sure to test all the edge cases. For example, 80% is a `B`, but 79.99…% is a `C`.

3. The last code sample checks if a username is valid, but it’s not working yet. Add `print` statements as directed to find and fix the logic errors.

1. Must be 5 - 10 characters long.
2. Must only contain letters and numbers.
3. Must contain at least 1 digit.

Test names:

1. `"R2D2"` should be invalid (too short).
2. `"CoderGirl"` should be invalid (no number).
3. `"rut*baga8"` should be invalid (illegal symbol).
4. `"This1IsTooLong"` should be invalid (too long).
5. `"High5"` and `"pyth0n"` are both valid (that’s a zero in place of the “o”).

Example

1. On line 10, add `print(is_valid)` to check if the conditional on line 8 correctly assigns `True` and `False` based on the length of the username. Be sure to run the program with all four test names. `R2D2` and `This1IsTooLong` should return `False`, while `CoderGirl` and `rut*baga8` should return `True`.

Is the conditional on line 8 doing its job correctly?

2. If `is_valid` is `False`, then the program should reject the username. The `print` statement on line 10 also lets you compare the value of `is_valid` to the final result. For example:

```False
```

In this case, `is_valid` is `False` at line 10, but the username still gets labeled as valid. This tells you that a logic error follows line 10.

3. On line 18, add `print(char, is_valid, has_digit)`. Make sure to indent the statement the same amount as the `else` on line 16.

 ```16 17 18``` ``` else: is_valid = True print(char, is_valid, has_digit) ```
4. Run the program again with all 4 test names. Note how the values of `is_valid` and `has_digit` change each time the loop repeats. Use the output to find and fix the logic error in the loop.

5. Hints:

1. The loop assigns `is_valid` to be `True` or `False` after every character in the username. Modify the code to preserve any `False` result.
2. There are at least two quick ways to accomplish this.

Bonus fix: The loop runs after the length check passes or fails. How can we make it so that the loop runs only if the length test passes?