Im trying to make a function in python to find an average out of a table.
In number_table
there are 4 items so I put the count to 4. The error says : sum += table[i] IndexError: list index out of range
.
Here is the code :
number_table = [1, 3, 5, 100]
def average(table, count):
sum = 0
for i in table:
sum += table[i]
return sum / count
print(average(number_table, 4))
Best pythonic and efficient ways:
def average(table):
return sum(table)/len(table)
or
from statistics import mean
def average(table):
return statistics.mean(table)