Compound boolean expressions, Challenge: Input validation
I'm stuck on 'Step 2' of the Challenge: Input validation in Lesson 2 of Unit 2 in Intro to computer science - Python.
Amy tests her form by entering 6 for the month and 31 for the day. What bug does she find?
June 31 should be an invalid date, because April, June, September, and November only have 30 days. She needs to validate the day and month in combination!
-
Add a conditional that checks if the day is 31 for the months with only 30 days.
-
If the combination is invalid, print the message:
Error. Day must be within the month.
Pay attention to your compound condition's order of operations - you may need parentheses here! You can ignore February for now.
Make sure to print the exact error message provided.
Following is the code they gave me to begin with:
day = int(input("Enter a day (1-31): "))
if day < 1 or day > 31:
print("Error. Day must be between 1 and 31.")
month = int(input("Enter a month (1-12): "))
if month < 1 or month > 12:
print("Error. Month must be between 1 and 12.")
According to Microsoft Copilot, this is how:
Log ind for at efterlade en kommentar.