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.

none of the codes worked that u gave me try the best and hardest you can i know u can do it bot

1 answer

Let's tackle both parts of the activity you mentioned: the proactive part with the calculator program and the reactive part with the sum of even numbers program.

Proactive Section: Python Calculator Program

Here’s how you can incorporate a while loop and try-except block to ensure valid decimal number inputs for a calculator.

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

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

# Now you can continue with the calculator operations
print(f"You entered {num1} and {num2}.")
# Add your calculator operations here

Testing the Proactive Code

  • When running the code, if you input letters or symbols (like "abc" or "@#$"), you should see the message: "That's not a valid number! Please enter a decimal number."
  • When you input valid decimal numbers like 5.5 or 2.3, it should accept those values and break out of the loop.

Reactive Section: Sum of Even Numbers Program

Let's look at the code provided and identify the bugs and how to fix them.

Original Program:

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

Issues in the Original Code:

  1. The loop condition while x 10: is missing a relational operator (it should be while x <= 10: to include 10 in the calculation).
  2. The variable name sum should not be used as it shadows a built-in function in Python. It's better to use a different variable name.

Fixed Code:

x = 0
total_sum = 0  # renamed from sum to total_sum
while x <= 10:  # Fixed the condition
    if x % 2 == 0:  # Only sum even numbers
        total_sum += x 
    x += 1
print(total_sum)

Explanation of the Fixes:

  • Condition Fix: Changing while x 10: to while x <= 10: ensures that the loop iterates until x is 10, thus including all numbers up to and including 10.
  • Variable Name Change: Renaming sum to total_sum prevents conflicts with the built-in sum() function in Python, making the code clearer and avoiding potential errors.

Trace Table for the Reactive Section

| Iteration | x | Is x Even? | total_sum (Cumulative) | |-----------|---|------------|--------------------| | 0 | 0 | Yes | 0 | | 1 | 1 | No | 0 | | 2 | 2 | Yes | 2 | | 3 | 3 | No | 2 | | 4 | 4 | Yes | 6 | | 5 | 5 | No | 6 | | 6 | 6 | Yes | 12 | | 7 | 7 | No | 12 | | 8 | 8 | Yes | 20 | | 9 | 9 | No | 20 | | 10 | 10| Yes | 30 |

Final Summary

The bugs identified were:

  1. The condition for the loop was incorrect and needed an operator.
  2. The use of the variable name sum was inappropriate as it conflicted with Python's built-in function.

Through these modifications, the program now correctly computes the sum of even numbers from 0 to 10, resulting in the output 30.

Once you implement these changes, you should see the desired outputs and behavior.