Python Practice › If-Else Programs › Program 58
Day name from a number
Write a Python program that reads a number from 1 to 7 and prints the day (1 = Sunday, 2 = Monday, … 7 = Saturday).
Input
7
Output
Enter number: 7 Saturday
Approach
- Validate the range, then index into a list of day names.
Solution
days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]n = int(input("Enter number: "))if 1 <= n <= 7: print(days[n - 1])else: print("Enter a number from 1 to 7")Outputtyped input: 7
Enter number: 7 Saturday
Line by line
Line 4
List indexes start at 0, so subtract 1.
Tip
A list lookup replaces seven elif branches.
Learn the concept
New to this topic? Read the lesson, then come back.