我試圖弄清楚我使用我的代碼時遇到的問題。 我想輸出導致:python 3從列表中增加元素並打印總數
Enter the high integer for the range 100
Enter the low integer for the range 20
Enter the integer for the multiples 15
List was created
The list has 5 elements.
90 75 60 45 30
Average of multiples is 60.00
我可以計算出「名單被創造」的一部分....但它說:「名單有5種元素。」在我的代碼中,它一直返回30而不是5.我想知道是否有人可能會指向正確的方向或部分返回正確的值。我非常感謝你在這件事情上的幫助。
def main():
x = int(input('Enter the high integer for the range: '))
y = int(input('Enter the low integer for the range: '))
z = int(input('Enter the integer for the multiples: '))
mylist = show_multiples(x,y,z)
show_multiples(x,y,z)
show_list(mylist)
def show_multiples(x,y,z):
mylist = []
for num in range(x,y,-1):
if num % z == 0:
mylist.append(num)
return mylist
print ('List was created')
def show_list(mylist):
total = 0
for value in mylist:
total += value
average = total/len(mylist)
print ('The list has', value, 'elements.')
print (mylist,end=' ')
print()
print ('Average of multiples is', format(average, '.2f'))
main()
你爲什麼叫'show_multiples'兩次? – mgilson 2014-09-02 19:48:22
另外,如果你想要真正執行該行,'print('List was created')'should should be _before_ return statement .-) – mgilson 2014-09-02 19:48:58