FInd The Bug Challenge
Scofield Idehen
Posted on June 13, 2023
def calculate_average(numbers):
total = 0
count = 0
for number in numbers:
total += number
count += 1
average = total / count
return average
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
result = calculate_average(numbers)
print(f"The average is: {result}")
Explanation
The code defines a function called calculate_average that takes a list of numbers as input. It initializes total and count variables to zero. Then, it iterates over the numbers in the list and adds each number to the total variable while incrementing the count by 1.
After the loop, the average is calculated by dividing the total by the count. Finally, the calculated average is printed to the console.
The challenge is to find a tiny mistake in the code that has a significant impact on the output. Your task is to identify and fix the bug to obtain the correct average. Good luck with the bug hunt!
💖 💪 🙅 🚩
Scofield Idehen
Posted on June 13, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
python Day- 2 Given an integer array arr, count element x such that x + 1 is also in arr.
April 9, 2020