livepython.net

305. Python Program to Write a Number in Words

HardPhase 3 · Challenge

Several small steps, all with tools you know: lists, //, % and if. Write 342, 805 and 610 in words on paper first, and notice which words you need.

Overview

Banks print amounts in words on cheques, and talking apps read numbers out loud. The trick is to split the number into parts and look the words up in lists.

Problem Statement

Selin builds a talking calculator for older people who cannot read small numbers well. Read a whole number from 0 to 999 and print it in English words, all in small letters:

  • 0 is zero.
  • 1 to 19 have their own words: one, two ... nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen.
  • The tens are twenty, thirty, forty, fifty, sixty, seventy, eighty and ninety. Note that it is forty, with no "u".
  • From 21 to 99, a number that is not a whole ten gets a hyphen (a short dash, -): forty-two, ninety-nine.
  • Hundreds come first: three hundred. Then a space and the rest, if there is a rest: three hundred forty-two, six hundred ten.

Do not use the word "and". Write three hundred forty-two, not "three hundred and forty-two".

Examples

Example 1. Input 342 gives:

three hundred forty-two

Why: 342 is 3 hundreds and 42. 42 is forty and two, joined by a hyphen.

Example 2. Input 805 gives:

eight hundred five

Why: 805 is 8 hundreds and 5. The tens digit is 0, so there is no tens word and no hyphen.

Input

One whole number from 0 to 999. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Output

One line with the number in words, in small letters. There is exactly one space between words, and no space around a hyphen. The tests try 10 different inputs.

Try it first. The solution opens when you solve this question, or after 3 tries with Submit.

Finish this chapter firstChapter 9 · Python Lists
solution.py
# write your code here

EXPECTED OUTPUT

1three hundred forty-two

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.