I'm new to programming. I tried to find a way to convert numbers into letters with Python. I would like to receive some advice to improve myself. This program works for numbers between 1 and 106 and I would like to know how the logic of the program can be improved.
def changeNumberIntoLetter(value):
number=numToLetter(value)
return number
def numToLetter(value): #The function converts the numbers into letters.
if value==1: return 'one'
elif value==2: return 'two'
elif value==3: return 'three'
elif value==4: return 'four'
elif value==5: return 'five'
elif value==6: return 'six'
elif value==7: return 'seven'
elif value==8: return 'eight'
elif value==9: return 'nine'
elif value==10: return 'ten'
elif value==11: return 'eleven'
elif value==12: return 'twelve'
elif value==13: return 'thirteen'
elif 13<value<=19: return composeTeen(value)
elif value>19:
if value==20: return 'twenty'
elif value==30: return 'thirty'
elif value==50: return 'fifty'
elif value==10**2: return 'one hundred'
elif value==10**3: return 'one thousand'
elif value==10**5: return 'one hundred thousand'
elif value==10**6: return 'one milion'
elif value>=20: return composeNumbers(value)
else: exit('Out of range')
else: return ''
def composeNumbers(value): #The function build every number biger than 40
if 40<=value<10**2:
value1=int(str(value)[0])
value2= int(str(value)[1])
if value1==2:
value1= 'twen'
return value1 + 'ty' + '-' + numToLetter(value2)
if value1==3:
value1='thir'
return value1 + 'ty' + '-' + numToLetter(value2)
if value1==8:
value1='eigh'
return value1 + 'ty' + '-' + numToLetter(value2)
elif value1==5:
value1='fif'
return value1 + 'ty' + '-' + numToLetter(value2)
return numToLetter(value1) + 'ty' + '-' + numToLetter(value2)
elif 10**2<=value<10**3:
value1=int(str(value)[0])
value2= int(str(value)[1:])
return numToLetter(value1) + ' ' + 'hundred' + ' ' + numToLetter(value2)
elif 10**3<=value<10**4:
value1=int(str(value)[0])
value2=int(str(value)[1:])
elif 10**4<=value<10**5:
value1=int(str(value)[0:2])
value2=int(str(value)[2:])
elif 10**5<=value<10**6:
value1=int(str(value)[0:3])
value2=int(str(value)[3:])
return numToLetter(value1) + ' ' + 'thousand' + ' ' + numToLetter(value2)
def composeTeen(value): #The function takes the unit and then converts it into letter to build the word.
value= int(str(value)[-1]) #It turns elem in string to take the last position and it converts it again in integer to change it in letters. Then it composes the word adding 'teen' at the end.
value= numToLetter(value)
if value=='five': value= 'fif'
value= value + 'teen'
return value