Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Speech Module by tarjeii
NUMBERS = {0:"zero", 1:"one", 2:"two", 3:"three",
4:"four", 5:"five", 6:"six", 7:"seven",
8:"eight", 9:"nine", 10:"ten", 11:"eleven",
12:"twelve", 13:"thirteen", 14:"fourteen", 15:"fifteen",
16:"sixteen",17:"seventeen",18:"eighteen", 19:"nineteen",
20:"twenty", 30:"thirty", 40:"forty", 50:"fifty",
60:"sixty", 70:"seventy", 80:"eighty", 90:"ninety", 100:"hundred"}
def checkio(number):
text = []
if number == 0:
text.append(NUMBERS[0])
return "".join,text
digit=number//100
if digit != 0:
text.append(NUMBERS[digit]+" "+NUMBERS[100])
digit=number%100
if 10 <= digit <= 19:
text.append(NUMBERS[digit])
else:
if digit >= 20:
text.append(NUMBERS[(digit//10)*10])
if (digit%10) != 0:
text.append(NUMBERS[digit%10])
return " ".join(text)
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert checkio(4) == 'four', "1st example"
assert checkio(133) == 'one hundred thirty three', "2nd example"
assert checkio(12) == 'twelve', "3rd example"
assert checkio(101) == 'one hundred one', "4th example"
assert checkio(212) == 'two hundred twelve', "5th example"
assert checkio(40) == 'forty', "6th example"
assert not checkio(212).endswith(' '), "Don't forget strip whitespaces at the end of string"
Oct. 26, 2015