Home > Software design >  calucalting items in a list
calucalting items in a list

Time:04-09

I'm trying to get input from the user and put it into a list then calculate the total but I keep getting an error "not iterable" there is the code:

def sales_function():
    month_list = ['January','February','March','April','May','June','July','August','September','October','November','December']
    month_sale = []
    for months in month_list :
        user_input = int(input(print("Enter the sales for "   months  ":" )))
        month_sale.append(user_input)
    for items in range(len(month_sale)):
        total_sales = sum(items)
        print("Total sales:"   total_sales)

sales_function()

CodePudding user response:

There are 2 problems: the print in the input and the use of the sum: the argument of sum is an iterable (list, tuple, ...) but you provide items which is an integer. Here is the code without these errors:

def sales_function():
    month_list = ['January','February','March','April','May','June','July','August','September','October','November','December']
    month_sale = []
    for months in month_list :
        user_input = int(input("Enter the sales for "   months  ":" ))
        month_sale.append(user_input)
    total_sales = sum(month_sale)
    print("Total sales:", total_sales)

CodePudding user response:

a tab indentation is needed after defining the function you want to write to tell python that the code after the definition is the code of the function

tip: you should call the lists you use with a plural name so, for example month_list becomes months_list: this way you can iterate on the list like this for month in months_list : and refer to each month as month and not months

you don't need to call the print function when using input, just pass the string you want to print to it

also you don't really want to cast an input directly to input, if for example the user input a string "Sup" instead of an integer the int cast throws an exception and the program crashes if you don't handle it in a try catch statement (it throws an exception even if the user input a float instead of an int ("1.1" --> crash))

for the other foreach loop you don't need the range(len()) thing, just iterate 'for month_sale in month_sales`

after this i'm going to guess that you wanted to print the sum of all the month sales so:

def sales_function():
    months_list = ['January','February','March','April','May','June','July','August','September','October','November','December']
    month_sales = []
    for month in months_list :
        user_input = int(input("Enter the sales for "   month  ":" ))
        month_sales.append(user_input)
    
    total_sales = 0
    for month_sale in month_sales:
        total_sales = total_sales   month_sale
    print("Total sales:"   str(total_sales))

CodePudding user response:

i have a solution for you.

   def sales_function():
month_list = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
              'November', 'December']
month_sale = []
for months in month_list:
    user_input = input("Enter the sales for "   months   ":")
    month_sale.append(user_input)
print("Total sales:", len(month_sale))
  • Related