Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
if and dict solution in Clear category for Speech Module by Olpag
n2w = {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', 0 : 'zero'}
def checkio(number):
if number < 20:
return n2w[number]
if number < 100:
if number % 10 == 0:
return n2w[number]
else:
return n2w[number // 10 * 10] + ' ' + n2w[number % 10]
if number < 1000:
if number % 100 == 0:
return n2w[number // 100] + ' hundred'
else:
return n2w[number // 100] + ' hundred ' + checkio(number % 100)
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"
print('Done! Go and Check it!')
March 5, 2019
Comments: