python 3从列表中添加元素并打印总计的值

时间:2014-09-02 19:42:00

标签: python function python-3.x printing element

我试图找出我的代码遇到的问题时遇到了不好的地方。 我想输出结果:

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()

1 个答案:

答案 0 :(得分:1)

看起来你只是打印错误的值:

print ('The list has', value, 'elements.')

应该是:

print ('The list has', len(mylist), 'elements.')