0
print ('Welcome to BirthPath Calculator')

Day = input('Input you day of Birth (1-31): ')
Month = input('Input your month of birth (1-12): ')
Year = input ('Input your year of birth: ')

Day = int(Day)
Month = int(Month)
Year = int(Year)

Birthpath = Day + Month + Year
sum(Birthpath)

print ('This is your Birth Path: '+str(Birthpath))

I want the Birthpath to sum to a single number. Let's assume that the value of the Birthpath is 2014, I want to sum it up to 2+0+1+4= 7.

martineau
  • 119,623
  • 25
  • 170
  • 301

3 Answers3

0

It's an old classic problem to get a sum of digits of a number:

def sum_of_digits(number):
    sum = 0
    while number > 0:
      remainder = number % 10
      sum += remainder
      number //= 10
    return sum
Ankit Jaiswal
  • 22,859
  • 5
  • 41
  • 64
0

It is possible to treat a string as a list. So if you make the total of the day+month+year into a string and then loop over that it that would work

print('Welcome to BirthPath Calculator')
day = int(input('Input you day of Birth (1-31): '))
month = int(input('Input your month of birth (1-12): '))
year = int(input('Input your year of birth: '))
total = day + month + year
birthpath = 0
for digit in str(total):
    birthpath += int(digit)
print('This is your Birth Path: ' + str(birthpath))

You can also use a list comprehension to make this a bit shorter.

print('Welcome to BirthPath Calculator')
day = int(input('Input you day of Birth (1-31): '))
month = int(input('Input your month of birth (1-12): '))
year = int(input('Input your year of birth: '))
total = day + month + year
birthpath = sum(int(digit) for digit in str(total))
print('This is your Birth Path: '+str(birthpath))
Thomas David Baker
  • 1,037
  • 10
  • 24
0

This simple one liner will work!

Birthpath = 2014
sum(int(i) for i in str(Birthpath))
ikuamike
  • 339
  • 1
  • 6