1

#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

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by anthology
0

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.

0

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.

0

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
0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.