why does this code not work

def average(one, two, three):
avg = one + two + three
return avg

print("The average of 6, 7, and 8 is:", average(6, 7, 8))

1 answer

The code you provided defines a function to calculate the average of three numbers but does not actually calculate the average correctly. You need to divide the sum of the three numbers by 3 to compute the average.

Here’s the corrected version of the code:

def average(one, two, three):
    avg = (one + two + three) / 3
    return avg

print("The average of 6, 7, and 8 is:", average(6, 7, 8))

In this code, the sum of the numbers (one + two + three) is divided by 3, which gives you the actual average of the three numbers. Now, when you run this code, it will output the correct average.