简体   繁体   中英

How to sum up all numbers contained in user input from a list

I am searching for an algorithm, which gets user input with a few numbers and then the result should be the sum of all those numbers printed on the console.
I know how to make the list and print it, but I can't select each number and add them together to get a final result.

costs = input('Enter the exspenses: ')                                      
costlist = costs.split()                                                    
for each_number in costlist:                                                  
  total = (int(costlist.index(each_number))+1)+                                
  print()

I know that something doesn't need to be here and that the total string isn't finished, but I think I'm somewhere close to the solution.

To calculate your total, just use sum() built-in function like this:

costs = input('Enter the exspenses: ')
costlist = costs.split()
costlist = [int(i) for i in costlist]
total = sum(costlist)

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