简体   繁体   English

python typeerror int对象不可迭代,求和函数问题(我认为)

[英]Python typeerror int object not iterable, sum function issue (I think)

The object of the program is to ask the user how many articles of clothing they collected on each day of a 3 day (weekend) clothes drive, average them, do it again for the second weekend, then average the two weekends (clothes per day). 该程序的目的是询问用户在3天(周末)的衣服驾驶中每天收集了多少衣服,将它们平均,在第二个周末再次进行,然后平均两个周末(每天的衣服) )。

Here is my code: 这是我的代码:

import math

num_clothes = int() 
weekend_total = int() 
weekend_avg = float() 
total_clothes = int() 
total_avg = float() 
index = int()


index = 1 
while index <= 2: 
    index = 1 
    while index <= 3: 
        num_clothes = int(input("How many articles of clothing did you collect today? ")) 
        index = index + 1

    weekend_total = sum(num_clothes) 
    weekend_avg = weekend_total / 3 
    print("Total Collected:\t", weekend_total) 
    print("Weekend Average:\t", weekend_avg) 
    index = index + 1`1

total_clothes = sum(weekend_total) 
total_avg = total_clothes / 6 
print("Total Number of Clothing Collected:\t", total_clothes) 
print("Average Collected:\t", total_avg)

And here is the error i keep getting: 这是我不断得到的错误:

Traceback (most recent call last):
  File "G:\ITCS 1140\labs\python\lab 9.py", line 17, in <module>
    weekend_total = sum(num_clothes)
TypeError: 'int' object is not iterable

I am trying to make num_clothes into a list and add all the values of it with sum(num_clothes). 我试图使num_clothes到列表中,并用sum(num_clothes)添加它的所有值。

Your num_clothes is an variable of type int and you are passing this variable as parameter to the sum function 您的num_clothes是int类型的变量,并且您将此变量作为参数传递给sum函数。

weekend_total = sum(num_clothes)

You can see by the documentation that, if you want to sum properly, you need an iterator passed as parameters. 您可以从文档中看到,如果要正确求和,则需要一个作为参数传递的迭代器。

If you want to read a list from the user, try to change your input to this: 如果要从用户读取列表,请尝试将输入更改为此:

    a = [int(x) for x in input().split()]  

Example
-------

    >>> a = [int(x) for x in input().split()]
    3 4 5
    >>> a
    [3, 4, 5]
    >>> sum(a)
    12
    >>>

Source 资源

Alternatively, you can leave your code as it is and change num_clothes to list(). 或者,您可以保留您的代码不变,并将num_clothes更改为list()。 Then, after read the input, append the result to the list: 然后,在读取输入后,将结果追加到列表中:

num_clothes = list()

...

num_clothes.append(int(input(...))

...

weekend_total = sum(num_clothes)

sum() is a function that adds all objects of an iterable object such as a list sum()是一个添加可迭代对象(例如列表)的所有对象的函数

list = [1,2,3]
sum(list)

>>> 6

either change num_clothes to a list by using .append() and adding an integer to the list every iteration of the while loop 通过使用.append()num_clothes更改为列表,并在while循环的每次迭代中将整数添加到列表中

or just make num_clothes a total by doing += int(input(..etc)) 或者通过做+= int(input(..etc))使num_clothes总数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM