Print Digit Word
Convert one digit to its English word with a Python list. Beginners practice zero-based indexing and validate a number before using it as a list position.
1# Take digit2digit = int(input("Enter a digit (0-9): "))34# Map to word5words = ["Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]67if 0 <= digit <= 9:8 print(words[digit])9else:10 print("Invalid digit")
Output
Enter a digit (0-9): 5 Five Enter a digit (0-9): 0 Zero
In this one
- Align digits with indexes
- Validate before indexing
- Understand negative indexes
How it works
- 1
Create the ordered list of digit words.
- 2
Check that digit lies from zero through nine.
- 3
Print words[digit], otherwise report invalid input.
Edge cases
What does zero print?
What does zero print?
Zero, using list index zero.
Why reject -1 before indexing?
Why reject -1 before indexing?
Python would otherwise select the last item, Nine.
What's going on
The list stores ten words in digit order, starting with Zero at index zero. That deliberate alignment lets words[digit] act as a small lookup table.
Before indexing, the condition requires a value from zero through nine. Input 5 selects the sixth list entry, Five. The guard matters because negative indexes are valid Python syntax but would select words from the end instead of rejecting the digit.
What does zero print? Zero, using list index zero.
Why reject -1 before indexing? Python would otherwise select the last item, Nine.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which expression retrieves the word?
Pick an answer to check it.
Question 2
What does zero print?
Pick an answer to check it.
Questions people ask
What does zero print?
Zero, using list index zero.
Why reject -1 before indexing?
Python would otherwise select the last item, Nine.
Which expression retrieves the word?
words[digit].
๐Related Content
- ๐
Phase 1 - Learn Concepts
Review Phase 1 concepts and explanations
- ๐
Phase 1 - Quiz
Test your Phase 1 understanding with quiz questions
- ๐ป
Phase 1 - All Practice Problems
Explore all practice problems for Phase 1
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum