Day name from a number

EasyIf-Else Programs · 58 of 64

Write a Python program that reads a number from 1 to 7 and prints the day (1 = Sunday, 2 = Monday, … 7 = Saturday).

Advertisement
Input
7
Output
Enter number: 7
Saturday

Approach

  1. Validate the range, then index into a list of day names.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More if-else programs

← All 64 If-Else Programs