Converting numbers to words - Python 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&lt;value&lt;=19: return composeTeen(value)
    elif value&gt;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&gt;=20: return composeNumbers(value)
        else: exit('Out of range')
    else: return ''

def composeNumbers(value):   #The function build every number biger than 40 
    if 40&lt;=value&lt;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&lt;=value&lt;10**3:
        value1=int(str(value)[0])
        value2= int(str(value)[1:])
        return numToLetter(value1) + ' ' + 'hundred' + ' ' + numToLetter(value2)
    elif 10**3&lt;=value&lt;10**4:
        value1=int(str(value)[0])
        value2=int(str(value)[1:]) 
    elif 10**4&lt;=value&lt;10**5:
        value1=int(str(value)[0:2])
        value2=int(str(value)[2:])
    elif 10**5&lt;=value&lt;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
This is good work! Well done as a beginner programmer. 


def changeNumberIntoLetter(value):
    number=numToLetter(value)
    return number



This function doesn't really do anything useful to be honest. Can't you directly use numToLetter.


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'



Instead of creating lot of if statements. Try using a dictionary like this:

NUM_TO_WORD_MAPPING = {1: "one", 2: "two"}


and you can refer the string to number using 

if value in NUM_TO_WORD_MAPPING:
    return NUM_TO_WORD_MAPPING[value]
else:
    # ... special cases.


As sergiy-kolodyazhnyy said, you can also use a list or a tuple. 


int(str(value)[-1])



use value % 10 to extract numbers easily. This is modulo operator and returns remainder after division. 



Bonus Content: Code Style

How your code looks like is as equally important as how you implement it. Because you and other programmers will spend time reading it.


  “Indeed, the ratio of time spent reading versus writing is well over
  10 to 1. We are constantly reading old code as part of the effort to
  write new code. ...[Therefore,] making it easy to read makes it easier
  to write.”
  
  ― Robert C. Martin, Clean Code: A Handbook of Agile Software
  Craftsmanship


Let's take a look at your function


def numToLetter(value): #The function converts the numbers into letters. 



I recommend that you use snake_case (number_to_letter) as it is considered industry standard for python programs. Comments are better done using """ comment """ style.

In this scenario however comment is not really required because your function name says that it's purpose is converting number to letter. People usually forget to update comments and it ends up giving incorrect information. If you want to use documentation comments anyway, make sure that comments are always up to date.

Reading material:


PEP-8 - Coding conventions / standard
PEP-287 - Document comment standard
Documentation related SO Question