139k views
5 votes
Write a program that asks the user to enter a month (1 for January, 2 for February, etc.).

a. Display the name of the month entered.
b. Display an error message if the user enters a number less than 1 or greater than 12.
c. Display the number of days in the month.

User ZoomIn
by
8.4k points

1 Answer

4 votes

Final answer:

A Python program that prompts the user for a month number and then displays the corresponding month's name and the number of days in that month. It also includes error handling for invalid input (numbers less than 1 or greater than 12).

Step-by-step explanation:

To write a program that interacts with the user to provide information about months and days, we can implement it in several programming languages. Below is a sample program in Python:

# Asks user to enter a month number
month = int(input("Enter a month (1 for January, 2 for February, etc.): "))

# Dictionary of months and their corresponding days
months = {
1: ("January", 31),
2: ("February", 28), # 29 on a leap year
3: ("March", 31),
4: ("April", 30),
5: ("May", 31),
6: ("June", 30),
7: ("July", 31),
8: ("August", 31),
9: ("September", 30),
10: ("October", 31),
11: ("November", 30),
12: ("December", 31)
}

# Check if month entered is valid
if 1 <= month <= 12:
month_name, days = months[month]
print("Month: ", month_name)
print("Number of days: ", days)
else:
print("Error: Please enter a number between 1 and 12.")

The program asks the user to input a month number. It then checks if the input is within the valid range of 1 to 12. If valid, it displays the month's name and number of days in that month; otherwise, it displays an error message. This simple program demonstrates how to interact with user input, perform validation, and respond with relevant information.

User Alexander Ibarra
by
7.4k points

No related questions found