min
and max
functions both take an iterable, and you are passing integer to them, that is the reason for the error. Furthermore, you are using keywords for your variables and you will get errors if you don't rename them.
You have too many problems with your algorithm, I will try to fix them, without completely destroying your code.
def get_average_in_range(my_list, low, high): # list is keyword, changed it to my_list
# min_val = min(low) - no need for these two lines, low and high are already low and high
# max_val = max(high)
my_range = range(min_val, max_val) # range is keyword, so I changed it to my_range
sum_num = 0
counter = 0 # for keeping count of numbers in range.
for number in my_range: # my_range is a range object, we use it
count = my_list.count(number) # getting how many times number is present in my_list
counter += count # increasing counter
sum_num += count # number #inc
avg = 0.0
if counter != 0: # if counter is 0, we keep average as it is
avg = sum_num / counter # all we need is counter
print(avg)
Actually this is not the best way, this is an awful way. We can optimize it:
def get_average_in_range(my_list, low, high):
sum_num = counter = 0
for number in my_list:
if low <= number < high:
counter += 1
sum_num += number
avg = 0.0
if counter != 0:
avg = sum_num / counter
print(avg)
The logic here is checking every number in my_list
to see if it is in range.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…