Say
From Exercism PythonMITOriginal pagePython1 class period1 exercise20 graded tests
Say
Introduction
Your friend Yaʻqūb works the counter at the busiest deli in town, slicing, weighing, and wrapping orders for a never-ending line of hungry customers. To keep things moving, each customer takes a numbered ticket when they arrive.
When it’s time to call the next person, Yaʻqūb reads their number out loud, always in full English words to make sure everyone hears it clearly.
Instructions
Given a number, your task is to express it in English words exactly as your friend should say it out loud. Yaʻqūb expects to use numbers from 0 up to 999,999,999,999.
Examples:
- 0 → zero
- 1 → one
- 12 → twelve
- 123 → one hundred twenty-three
- 1,234 → one thousand two hundred thirty-four
Exception messages
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")
Open the
01folder and editmain.py. Run Tests checks it.
For teachers
Say
Students solve Exercism's “Say” Python exercise in CS Room, with Exercism's own tests.
- Time: 1 class period
- Tests: 20, set up for you when you import the lesson