-
Notifications
You must be signed in to change notification settings - Fork 139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
python #14
Comments
To create a "magic date" function in Python that takes input from the user, you can use the datetime module. A magic date is a date where the day multiplied by the month equals the two-digit year. Here's an example of how you can implement this function: from datetime import datetime
def is_magic_date(date_string):
try:
date = datetime.strptime(date_string, "%m-%d-%y")
day = date.day
month = date.month
year = date.year % 100
if day * month == year:
return True
else:
return False
except ValueError:
return False
# Get date input from the user
date_input = input("Enter a date in MM-DD-YY format: ")
# Check if the date is magic
if is_magic_date(date_input):
print("It's a magic date!")
else:
print("It's not a magic date.") In this code, you define the The function then checks if the day multiplied by the month equals the year. If they are equal, it returns Finally, we get the date input from the user using the |
def is_magic_date(date_str):
Get the date input from the userdate_input = input("Enter a date in the format DD/MM/YY: ") Check if it's a Magic Date and print the resultif is_magic_date(date_input): |
Your Name: Hajra Chughtai
Program Name: Hajra-Homework3A.py
What the program does: This program tests if a date is a Magic Date or not.
Function to check if a date is a Magic Date
def is_magic_date(day, month, year):
return day * month == int(str(year)[-2:])
Input from the user
day = int(input("Enter the day (1-31): "))
month = int(input("Enter the month (1-12): "))
year = int(input("Enter the year (4-digit): "))
Check if the entered date is a Magic Date
if is_magic_date(day, month, year):
print(f"{day}/{month}/{year} is a Magic Date!")
else:
print(f"{day}/{month}/{year} is not a Magic Date.")
input('Press Enter to exit')
The text was updated successfully, but these errors were encountered: