python - converting iteration into a function -


i'm beginner in python, , computer languages in general, , i'm totally stumped on how format iteration function. iteration takes sum of someone's birth year, month, , day , adds create sum of numbers, , second iteration takes first sum , adds numbers create final sum. this

i have users input birthyear, month, , day (all converted int) , code first sum (example: bday of 01/01/1997= 1999):

first_sum=b_yr + b_dy + b_mo 

then first iteration takes sum , adds numbers (example: 1999 = 1+9+9+9 = 28):

z = first_sum zstr1=str(z) accum1=0 x in zstr1:    accum1 += int(x) (accum1) 

then second iteration takes first sum , adds numbers again create final sum (example: 28 = 2+8 = 10):

str2=str(accum1) accum2=0 cnt2 in str2:     accum2 += int(cnt2) 

i think should work:

def numerology(z):     zstr1=str(z)     accum1=0     x in zstr1:         accum1 += int(x)     str2=str(accum1)     accum2=0     x in str2:         accum2 += int(x)     return accum2 

call function

numerology(first_sum) 

btw strange way of doing (to me)

edit: add anonymous's functional way, recursive 1 (with list comprehension).

def numerology(z):     zstr1=list(str(z))     res=sum([int(i) in zstr1])     if res>10:         res=numerology(res)     return res 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -