Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
149 views
in Technique[技术] by (71.8m points)

python - Trying to write a function but int object is not iterable

I'm currently trying to write a function that accepts a list of integers, a low integer, and a high integer. It then will return the average of the values in the list of integers that lie within the range of the low and high integers.

An example of this might be get_average_in_range([1, 5, 6, 7, 9], 5, 7) and the answer returns as 5.5 because only 5 and 6 are within the range of 5 (inclusive) and 7 (exclusive) and 5 and 6 averaged is 5.5

I'm currently getting an error that 'int' object is not iterable

Here's my code so far:

def get_average_in_range(list, low, high):
    min_val = min(low)
    max_val = max(high)
    range = range(min_val, max_val)
    sum_num = 0
    for number in range(range):
        sum_num = sum_num + x
    avg = sum_num / len(list)
    print(avg)
question from:https://stackoverflow.com/questions/65836872/trying-to-write-a-function-but-int-object-is-not-iterable

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...