Given a number from 0 to 999,999,999,999, spell out that number in English.
Handle the basic case of 0 through 99.
If the input to the program is 22
, then the output should be 'twenty-two'
.
Your program should complain loudly if given a number outside the blessed range.
Some good test cases for this program are:
If you're on a Mac, shell out to Mac OS X's say
program to talk out loud.
If you're on Linux or Windows, eSpeakNG may be available with the command espeak
.
Implement breaking a number up into chunks of thousands.
So 1234567890
should yield a list like 1, 234, 567, and 890, while the far simpler 1000
should yield just 1 and 0.
Now handle inserting the appropriate scale word between those chunks.
So 1234567890
should yield '1 billion 234 million 567 thousand 890'
The program must also report any values that are out of range. It's fine to stop at "trillion".
Put it all together to get nothing but plain English.
12345
should give twelve thousand three hundred forty-five
.
The program must also report any values that are out of range.
Sometimes it is necessary to raise an exception. When you do this, you should always include a meaningful error message to indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. For situations where you know that the error source will be a certain type, you can choose to raise one of the built in error types, but should still include a meaningful message.
This particular exercise requires that you use the raise statement to "throw" a ValueError
if the number input to say()
is out of range. The tests will only pass if you both raise
the exception
and include a message with it.
To raise a ValueError
with a message, write the message as an argument to the exception
type:
# if the number is negative
raise ValueError("input out of range")
# if the number is larger than 999,999,999,999
raise ValueError("input out of range")
Sign up to Exercism to learn and master Python with 17 concepts, 140 exercises, and real human mentoring, all for free.