Home > Blockchain >  calculating sum of items in a list
calculating sum of items in a list

Time:04-10

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:

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

CodePudding user response:

A tab indentation is needed after defining the function you want to write, in order 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 (for example: month_list becomes months) and be able to iterate using the corresponding singular and plural form (for month in months:).

You don't need to call the print function when you want to take a look at the content of a variable: just pass the name of the variable you want to see in a new line.

Also you don't really want to cast an input directly to Python type. For example if the user inputs a string "Sup" instead of a required integer, Python will throw an exception when you try to cast "Sup" to an integer. This causes the end of the execution of the program (if you don't handle the exception manually using a try except statement).

For the other for loop you don't need range(len()), just use for month_sale in month_sales to carry out the iteration.

After this, I'm going to guess that you wanted to print the sum of all the month sales. Here it is:

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))
  • Related