For this activity, you will practice being both proactive and reactive to bugs. Both are necessary to get rid of errors in code.

Proactive:
Log in to OnlineGDB and open your Python Calculator program from Activity 2 in Unit 1.
Add in a while loop and a try except block around both lines where you input the decimal numbers:
num1 = float(input("Enter your first number: "))
num2 = float(input("Enter your second number: "))
You may want to refer back to the unit example where we used a while loop and try except block to make sure the user entered a valid age:

while True:
try:
num = int(input("Enter your age: "))
break
except ValueError: print("That's not a number!")
Test your program by typing in letters or symbols instead of numbers. Make sure you get an error message whenever you enter a non-number.
When you are finished, share the link to your web page with your teacher by clicking on the share button and copying the link.
Reactive:
The following Python program should print the sum of all the even numbers from 0 to 10 (inclusive), but it has two bugs. Use the debugger tool in OnlineGDB to find the bugs.

x = 0
sum = 0
while x 10:
sum = sum + x
x = x + 1
print(sum)
Using a word processing document, complete a trace table and then write a sentence or two explaining what the errors are and how to fix them. Submit your word processing document using this unit’s dropbox.

1 answer

Proactive: Enhancing the Python Calculator

To make the Python calculator robust, we can wrap the input statements in a while loop and a try-except block to handle invalid inputs. Below is the updated code for your calculator program:

while True:
    try:
        num1 = float(input("Enter your first number: "))
        break  # Break the loop if input is valid
    except ValueError:
        print("That's not a valid number! Please try again.")

while True:
    try:
        num2 = float(input("Enter your second number: "))
        break  # Break the loop if input is valid
    except ValueError:
        print("That's not a valid number! Please try again.")

# Continue with your calculator operations...

Testing the Proactive Code

To test the above code, you would input invalid values like letters or symbols (e.g., "abc", "!!"). You should see the error message "That's not a valid number! Please try again." until a valid number is entered.

Reactive: Detecting Bugs in the Given Code

Here is the provided buggy code:

x = 0
sum = 0
while x  10: 
    sum = sum + x 
    x = x + 1
print(sum)

Bugs Identified

  1. Syntax Error in the While Loop Condition: The condition while x 10: is incorrect. The operator that should be used (like <, >, <=, etc.) is missing. Hence, this will raise a syntax error.

  2. Using sum as a Variable Name: sum is a built-in function in Python. Using it as a variable name can lead to confusion and unintended issues. It's better to use a different name, such as total_sum.

Corrected Code

Here’s the fixed version of the program:

x = 0
total_sum = 0
while x <= 10:  # Corrected condition to include 10
    total_sum = total_sum + x 
    x = x + 1
print(total_sum)

Trace Table and Explanation

| Iteration | x | total_sum | Condition (x <= 10) | |-----------|---|------------|---------------------| | 1 | 0 | 0 | True | | 2 | 1 | 1 | True | | 3 | 2 | 3 | True | | 4 | 3 | 6 | True | | 5 | 4 | 10 | True | | 6 | 5 | 15 | True | | 7 | 6 | 21 | True | | 8 | 7 | 28 | True | | 9 | 8 | 36 | True | | 10 | 9 | 45 | True | | 11 | 10| 55 | True | | 12 | 11| 55 | False |

Explanation of the Errors

The primary errors in the initial code were related to a syntax issue due to a missing comparison operator in the while loop condition, which caused the program to fail to run. Additionally, using sum as a variable name creates confusion since it's reserved for the built-in Python function. The corrected code uses x <= 10 in the loop condition to include 0 to 10, and renames the sum variable to total_sum for clarity. After these corrections, the program now correctly calculates the sum of all even numbers from 0 to 10 and prints the result.

Submission

After creating your trace table and writing your explanation, ensure you save the word processing document and submit it using your unit's dropbox as directed.

If you have any questions or need further assistance, feel free to ask!