I am trying to find the average of numbers in ranges (i.e. find the average of all numbers in range 1-1000). I wrote the following code to do this, but the due to the if-statement
, when run, the code produces multiple numbers. I then tried a while-loop
instead, but when I entered a break
statement, it produced the same list
the if-statement
produced. Is there any way to use an if-statement
and get 1 single number that is the average? Thank you!
mylist =[]
def ave_data(x, y):
for line in filename:
for number in line.split():
if int(number) > x and int(number) < y:
mylist.append(int(number))
print(sum(mylist)/len(mylist))