I'm trying to create a functional for the average of a list that accepts three parameters: a mandatory list of numbers, an optional minimum value which defaults to zero, and an an optional maximum value which defaults to 100. The function should only calculate the average of values between the stated minimum and maximum, even if there are other vaues in the list.
def avgList(lst, low = 0, high = 100):
"""Calculates the average of a list of values"""
for i in range(low, high):
avg = sum(lst)/len(lst)
return avg
This is what I tried. I also have a few sample outputs I tried, it worked for the first two but not for the third. The first is asking for the mean of a list from 1 to 10
lst1 = list(range(1,11))
average1 = avglist(lst1)
average1
The second is asking for the mean of the numbers between 20 and 80 in a list from 1 to 100.
lst2 = list(range(1, 101))
average2 = avglist(lst2, 19, 80)
average2
The third is asking for a mean of all the numbers between 30 and 100 in a randomly generated list of 100 numbers between 1 and 100. I would expect the mean to be around 65, but it usually ends up around 50
lst3 = []
for i in range(1, 100):
n = random.randint(1, 100)
lst3.append(n)
average3 = avglist(lst3, 29, 100)
average3