Reputation: 13
This code is supposed to take the list from one function to the other. The list will only come with one element. I am a beginner in Python and need some help. The code works, but only brings one element from where the list was created.
When I output the code, I've been using the numbers high = 100
, low = 20
, and multi = 15
. I should have in my list [90, 75, 60, 45, 30]
. There should be 5 elements coming out of the show_mulitples
function as a list. I need to take that list into the show_list
function and count the elements, show the multiples, and get an average. But all that I get is the first element, 90.
def main():
#get the integers
high = int(input('Enter the high integer for the range '))
low = int(input('Enter the low integer for the range '))
multi = int(input('Enter the integer for the multiples '))
#call the function
multiples = show_multiples(low, high, multi)
list_info = show_list(multiples)
#take the arguments into the function
def show_multiples(low, high, multi):
#make empty list
multi_list = []
#make the list
for i in range(high, low, -1):
if i % multi == 0:
multi_list.append(i)
print('List was created')
return multi_list
#take the list into the function
def show_list(multiples):
#create empty total
total = 0.0
#add the list together
for value in multiples:
total += value
#get Average
avg = total / len(multiples)
print('This list has',len(multiples),'elements')
print(multiples)
print('The average of the multiples is',avg)
main()
Upvotes: 1
Views: 674
Reputation: 5971
In your code above, you are returning the list straight after the first element has been added to it. You need to move your return statement out of the loop (NB: In Python indenting is important!).
Try something like:
def show_multiples(low, high, multi):
#make empty list
multi_list = []
#make the list
for i in range(high, low, -1):
if i % multi == 0:
multi_list.append(i)
print('List was created')
return multi_list # <-- this should be out of the loop
Upvotes: 2