127k views
4 votes
Write a program named CheckMonth2 that prompts a user to enter a birth month and day. Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day entered is 17, the output should be 2/17 is a valid birthday.

User Zimex
by
5.1k points

1 Answer

3 votes

Answer:

#Welcome message

print("welcome to CheckMonth2")

#prompting day and month

day = int(input("Please, add a day: "))

month = int(input("please, add a month: "))

#Defining max number of days per each month

monthly_days = [31,29,31,30,31,30,31,31,30,31,30,31]

#Validating day and month were added properly

if month > 12 or month < 1:

print("invalid date for the month")

elif day > monthly_days[month-1] or day < 1:

#Printing error message

print("invalid date for the month")

else:

#printing correct message

print ("{}/{} is a valid birthday".format(day,month))

Step-by-step explanation:

This code was made using Python and consists of the following parts:

  • A Welcome message with the name of the code.
  • A line to prompt the day.
  • A line to prompt the month.
  • A list to define the number of day per month. For example, the first posistion of the list corrresponds to the maximum number of days in January, the next is for February and so on.
  • A conditional tests to validate the day and month are in correct. ranges.
  • Output the error message if conditions are not met.
  • Output date if contidios are met.
User Pahaz
by
5.4k points