In [ ]:
# If the numbers 1 to 5 are written out in words: 
#     one, two, three, four, five,
#     then there are 3 + 3 + 5 + 4 + 4 = 19 letters used 
#     in total.

# If all the numbers from 1 to 1000 (one thousand) 
# inclusive were written out in words, how many letters
# would be used?
In [50]:
def wordy_number(n):
    name = ""
    if n == 1000:
        return "one thousand"
    if n>99:
        hundred_digit = units[n/100-1]
        name += hundred_digit + " hundred"
        n = n%100
        if n == 0:
            return name
        else:
            name += ' and '
    if n<=10:
        name += units[n-1]
    elif n>10 and n<20:
        name += teens[n-11]
    if n>=20:
        unit_digit = n%10
        if unit_digit == 0:
            name += tens[(n/10)-2]
        else:
            name += tens[(n/10)-2] + ' ' + units[unit_digit -1]
    return name
In [48]:
def count_letters(limit):
    # returns the number of leters used to write all the numbers up to limit
    total_letters = 0
    for i in range(1,limit+1):
        word = wordy_number(i)
        total_letters += sum([len(w) for w in word.split(' ')])
    return total_letters
In [51]:
print count_letters(1000)
21124
In [ ]: