#After user inputs the year they were born I want to break it down using fadic addition like below#

birth_year = int(raw_input('Please enter the year you were born'))

#User inputs 1953

birth_year = 1953

#What I want is to do this using Fadic addition:

birth_number1 = 1 + 9 + 5 + 3 # = 27
birth_number2 = 2 + 7 = 9 # = 9

print 'Your birth year number is:', birth_number2

Recommended Answers

All 4 Replies

I suppose you want to continue the process until the number becomes lower then ten.

def fadic(birth_number):
    s=str(birth_number)
    res=10
    while res>9:
        res=sum(int(c) for c in s)
        s=str(res)
    return res

The function works even in the wild future too. I don't know what to do with dates before Christ.

Thank you Slate. That code works fine. Using 1953 returns 18.
I understand def fadic(birth_number): to define a function called fadic.
And s=str(birth_number)
This part: res=10
while res>9:
res=sum(int(c) for c in s)
Im not sure about could you explain this bit of code in detail for me Please.
the res and int (c) i've not learnt yet.
Thank you again for your time.

It should return 9 for 1953 and it does.

res: is the number which we will return
res=10 :put res so high that we can enter the loop

while res>9: we do looping while the return value is greater then the desired.

res=sum(int(c) for c in s)
we take each character (c) of the number(s), convert it to integer(int(c)) and sum that up.
it is equivalent to:

su=0
for c in s:
     su=su+int(c)
return su

def fadic(birth_number):
s=str(birth_number)
res=10
while res>9:
res=sum(int(c) for c in s)
s=str(res)
return res

print fadic(1953)

9

#Yes you are correct 9 is the result. Sorry.
# 1 + 9 + 5 + 3 = 18 1 + 8 = 9
#got it now Neat :)
#Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.