Nicolas Moran - 2 years ago 124
Python Question

# Python Sum individual integers

I want to make a python app that sums the numbers of a birthdate.

``````day = raw_input("What day were you born?")
month = raw_input("What month were you born?")
year = raw_input("What year were you born?")
day = int(day)
month = int(month)
year = int(year)
``````

Let's say that someone was born on 12/10/1928; how could I make this app to sum it like this: 1+2+1+0+1+9+2+8?
This is done on Python 2.7

PS: I forgot to mention that my final result must be a one digit number so for example in the sum above the result is 24 but that number must sum itself so 2 + 4 = 6, being 6 the final answer.

Instead of casting them as integers, add them as strings and then map each character as an integer and find the sum:

``````day = raw_input("What day were you born?")
month = raw_input("What month were you born?")
year = raw_input("What year were you born?")
print sum(map(int, day+month+year))
``````

If you want to keep adding the digits until you arrive at a one-digit number, use a loop:

``````day = raw_input("What day were you born?")
month = raw_input("What month were you born?")
year = raw_input("What year were you born?")
num = day+month+year
while len(num) > 1:
num = str(sum(map(int, num)))
print num
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download