Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more

Given a number between 0-99999, the function number_to_english, return the same number pass to argument in letters.

For example:

if pass 9 with argument of number_to_english this return nine

The task is very simple this code is very ugly, and need to refactor it.

def mil(n):
    num={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine'}
    dec={'10':'ten','11':'eleven','12':'twelve','13':'thirteen','14':'fourteen','15':'fifteen','16':'sixteen','17':'seventeen','18':'eighteen','19':'nineteen'}
    dec_com={'20':'twenty','30':'thirty','40':'forty','50':'fifty','60':'sixty','70':'seventy','80':'eighty','90':'ninety'}
    mil="thousand"
    th=[]
    n_t=[x for x in n]
    for i in n_t:
        if len(n_t)==2:
            if i!='1' and n_t[1]=='0':
                th.append(dec_com[i+'0'])
                th.append(mil)
                break
            elif i=='1':
                th.append(dec[i+n_t[1]])
                th.append(mil)
                break
            else:
                th.append(dec_com[i+'0'])
                th.append(num[n_t[1]])
                th.append(mil)
                break
        else:
            th.append(num[i])
            th.append(mil)
    return th
def cen(n):
    num={'0':'zero','1':'one','2':'two','3':'three','4':'four','5':'five','6':'six','7':'seven','8':'eight','9':'nine'}
    dec={'10':'ten','11':'eleven','12':'twelve','13':'thirteen','14':'fourteen','15':'fifteen','16':'sixteen','17':'seventeen','18':'eighteen','19':'nineteen'}
    dec_com={'20':'twenty','30':'thirty','40':'forty','50':'fifty','60':'sixty','70':'seventy','80':'eighty','90':'ninety'}
    cen="hundred"
    c=[]
    n_d=[x for x in n]
    for m in n_d:
        if n_d[0]!='0':
            c.append(num[m])
            c.append(cen)
            if n_d[1]=='0' and n_d[2]=='0':
                break
            elif n_d[1]=='0' and n_d[2]!='0':
                c.append(num[n_d[2]])
                break
            elif n_d[1]!='1' and n_d[2]=='0':
                c.append(dec_com[n_d[1]+'0'])
                break
            elif n_d[1]=='1':
                c.append(dec[n_d[1]+n_d[2]])
                break
            else:
                c.append(dec_com[n_d[1]+'0'])
                c.append(num[n_d[2]])
                break
        else:
            if n_d[1]=='0' and n_d[2]=='0':
                break
            elif n_d[1]=='0' and n_d[2]!='0':
                c.append(num[n_d[2]])
                break
            elif n_d[1]!='1' and n_d[2]=='0':
                c.append(dec_com[n_d[1]+'0'])
                break
            elif n_d[1]!='1' and n_d[2]!='0':
                c.append(dec_com[n_d[1]+'0'])
                c.append(num[n_d[2]])
                break
            elif n_d[1]=='1':
                c.append(dec[n_d[1]+n_d[2]])
                break
            
    return c
def number_to_english(n):
    num={0:'zero',1:'one',2:'two',3:'three',4:'four',5:'five',6:'six',7:'seven',8:'eight',9:'nine'}
    dec={10:'ten',11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'fifteen',16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen'}
    dec_com={20:'twenty',30:'thirty',40:'forty',50:'fifty',60:'sixty',70:'seventy',80:'eighty',90:'ninety'}
    th=[]
    c=[]
    m='{0:,}'.format(n)
    m=m.split(",")
    try:
        if n<0 or type(n)==float or n>99999:
            pass
        elif n<10:
            c.append(num[n])
        elif n<20:
            c.append(dec[n])
        elif n%10==0 and n<99:
            c.append(dec_com[n])
        elif n<99:
            k=list(str(n))
            c.append(dec_com[int(k[0]+'0')])
            c.append(num[int(k[1])])
        else:
            c=cen(m[1])
            th=mil(m[0])
    except IndexError:
        if n<0 or type(n)==float or n>99999:
            pass
        elif n<10:
            c.append(num[n])
        elif n<20:
            c.append(dec[n])
        elif n%10==0 and n<99:
            c.append(dec_com[n])
        elif n<99:
            k=list(str(n))
            c.append(dec_com[int(k[0]+'0')])
            c.append(num[int(k[1])])
        else:
            c=cen(m[0])
    t=[]
    t.extend(th)
    t.extend(c)
    return " ".join(t)