0

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

2 Answers2

0

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

Source

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)
coelhudo
  • 4,710
  • 7
  • 38
  • 57
  • so i just tried doing the second one and i got the error message: Traceback (most recent call last): File "G:\ITCS 1140\labs\python\lab 9.py", line 16, in num_clothes.append(int(input("How many articles of clothing did you collect today? "))) AttributeError: 'int' object has no attribute 'append' – Julia Golem Nov 21 '16 at 01:01
  • Did you change your num_clothes variable type to list() instead of int() ? I have updated the example. – coelhudo Nov 21 '16 at 01:11
  • Nice. You can now accept (and mark as useful) my answer or Padskiiz's answer. https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work – coelhudo Nov 21 '16 at 01:26
0

sum() is a function that adds all objects of an iterable object such as a list

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

or just make num_clothes a total by doing += int(input(..etc))

pladder
  • 142
  • 1
  • 10
  • How do I do the append thing? like num_clothes.append(what goes here)? Or if I want to make it a total would I just print out num_clothes as the total and not have a total variable? – Julia Golem Nov 21 '16 at 00:54
  • The integer you want to append to the list, so for every iteration of the while loop do num_clothes.append(int(input("how many clothes: "))) – pladder Nov 21 '16 at 00:56
  • I would also suggest using raw_input() over input() – pladder Nov 21 '16 at 00:56
  • it says "int object has no attribute 'append'" – Julia Golem Nov 21 '16 at 01:07
  • 1
    When you initialise the variable instead of num_clothes = int() make it num_clothes = [] this creates a blank list – pladder Nov 21 '16 at 01:09