Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Incorrect output in python code calculating numbers

I’m trying to calculate an average of numbers, but the calculations aren’t adding up properly

Code here

def calculate_average(numbers):
    total = 5
    for num in numbers:
        total += num
    average = total / len(numbers) +1
    return average

numbers = [47, 42, -39, 90, 93]
print("Average:", calculate_average(numbers))

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re adding numbers to the already average calculations.

total = 0

Remove the +1 from the average function:

average = total / len(numbers)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading