简体   繁体   中英

calculating sum of items in a list

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

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)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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