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.

Activity 1 Grading Rubric
Proactive Reactive
Full Credit
The try except statements and while loops have been correctly completed and placed in the right locations. An error message appears when the user enters in a non-number.

A trace table has been correctly completed. The two bugs have been identified and fixed. Both errors are thoroughly explained.

Partial Credit
The try except statements may contain a small error or two or may not be in the correct location. While loops may not be used correctly. An error message may not appear.

The trace table may contain several errors. Only one bug has been properly identified and fixed. The explanation may be weak.

Little Credit
The try except statements contains numerous errors and have not been placed in the correct locations. The while loops do not work.

The trace table contains numerous errors.

The two bugs are not correctly identified, and explanations are not included.

1 answer

Sure, let's start with the Proactive part first, which involves enhancing the Python Calculator program with error handling for input values. Here's a modified version of the input section in your calculator program:

while True:
    try:
        num1 = float(input("Enter your first number: "))
        break  # Exit the loop if the 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  # Exit the loop if the input is valid
    except ValueError:
        print("That's not a valid number! Please try again.")

Explanation

  • This code will keep prompting the user for a valid number until they enter one by using a while True loop combined with a try-except block.
  • If the user inputs something that's not a number (e.g., letters or symbols), a ValueError will occur, and the message "That's not a valid number! Please try again." will be printed.
  • The loop will continue until a valid float number is entered.

Testing

  • You should test the program by typing both valid and invalid inputs, making sure that invalid inputs trigger the error message without crashing the program.

Now, let's move on to the Reactive part of the task which involves fixing the provided buggy code. Here is the buggy code provided:

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

Identifying and Fixing Bugs

  1. Bug Identification:

    • The condition in the while loop is incorrect. It should use a comparison operator (<) instead of just stating x 10.
    • The range of numbers we're interested in should include the number 10.
  2. Corrected Code:

x = 0
sum = 0
while x <= 10:  # Corrected condition to include 10
    if x % 2 == 0:  # Only add even numbers to sum
        sum = sum + x 
    x = x + 1
print(sum)

Explanation

  • The while loop condition has been corrected to while x <= 10: so that the loop continues until x reaches 10, inclusive.
  • We added a condition if x % 2 == 0: to ensure that only even numbers are added to sum.

Trace Table

| Iteration | Value of x | Is x Even? | Sum after Adding | |-----------|------------|------------|------------------| | 1 | 0 | Yes | 0 | | 2 | 1 | No | 0 | | 3 | 2 | Yes | 2 | | 4 | 3 | No | 2 | | 5 | 4 | Yes | 6 | | 6 | 5 | No | 6 | | 7 | 6 | Yes | 12 | | 8 | 7 | No | 12 | | 9 | 8 | Yes | 20 | | 10 | 9 | No | 20 | | 11 | 10 | Yes | 30 |

Conclusion

  • The two bugs were the incorrect condition in the while loop and the failure to check for even numbers.
  • Fixing these errors allows the program to correctly calculate the sum of all even numbers between 0 and 10, which is 30.

Feel free to copy these explanations and code snippets into your document for submission. Good luck!