Im trying to write a function to get an average out of a table in python but I get an error saying 'List index out of range'
Question:
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))
Answers:
using:
for i in table:
returns the elements in the table. so if you access table[i] will go out of range in third iterations, since third element is 5 and your list has 4 elements.
what you are looking for is
number_table = [1, 3, 5, 100]
def average(table):
table_sum = 0
for i in range(len(table)):
table_sum += table[i]
return table_sum / len(table)
print(average(number_table))
note that i changed the variable sum to table_sum, because sum is a python function and you are overriding it (works but dangerous…..)
then you do not need the count, because you can divide by the length of the table which can be obtained using len(table)
With all the comments suggesting what to do, this is what you should be doing:
number_table = [1, 3, 5, 100]
print(sum(number_table)/len(number_table))
sum()
is a built-in function in Python which takes an iterable to do summations on. len()
is another built-in function that returns the length of an object (in this case the number of elements in the list), so you don’t have to hard-code this value which comes in handy when adding more values to the table. You should make use of them when possible, as it gives shorter and more readable code.
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)
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))
using:
for i in table:
returns the elements in the table. so if you access table[i] will go out of range in third iterations, since third element is 5 and your list has 4 elements.
what you are looking for is
number_table = [1, 3, 5, 100]
def average(table):
table_sum = 0
for i in range(len(table)):
table_sum += table[i]
return table_sum / len(table)
print(average(number_table))
note that i changed the variable sum to table_sum, because sum is a python function and you are overriding it (works but dangerous…..)
then you do not need the count, because you can divide by the length of the table which can be obtained using len(table)
With all the comments suggesting what to do, this is what you should be doing:
number_table = [1, 3, 5, 100]
print(sum(number_table)/len(number_table))
sum()
is a built-in function in Python which takes an iterable to do summations on. len()
is another built-in function that returns the length of an object (in this case the number of elements in the list), so you don’t have to hard-code this value which comes in handy when adding more values to the table. You should make use of them when possible, as it gives shorter and more readable code.
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)