Julia Golem - 12 days ago 7

Python Question

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

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

Answer

Your num_clothes is an variable of type **int** and you are passing this variable as parameter to the sum function

```
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
>>>
```

Alternatively, you can leave your code as it is and change num_clothes to 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)
```

Source (Stackoverflow)

Comments