Python Array Solution Q8
Create an array of 12 integers representing the months of the year. Ask the user to enter a month number (1 for January, 2 for February, etc.) and print the corresponding month.
from array import array
# Array representing months of the year
months_array = array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
# Take input from the user for a month number
user_input = int(input("Enter a month number (1 for January, 2 for February, etc.): "))
# Validate the user input
if 1 <= user_input <= 12:
# Find the corresponding month and print it
corresponding_month = months_array[user_input - 1]
print(f"The corresponding month is: {corresponding_month}")
else:
print("Invalid month number. Please enter a number between 1 and 12.")
In this program:
An array named months_array is created with integers representing the months of the year.
The user is prompted to enter a month number.
The program validates the user input to ensure it's within the valid range (1 to 12).
If the input is valid, it finds and prints the corresponding month from the array.
Users can enter a month number, and the program will output the corresponding month.
Comments
Post a Comment
If you have any doubts, Please let me know .