简体   繁体   中英

how can i make a sum in a line of numbers in python?

i'm in trouble trying to calculate and appear the sum of a series of numbers in lines. it should be like this:

32565854 SUM: 38

65985456 SUM: 48

17984956 SUM: 49

you can turn the line into string and then:

sum(map(int, '32565854'))

with a file:

with open('input.txt') as f:
    for line in f:
        line = line.strip()
        print line + ' ' + str(sum(map(int, line)))

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