Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Coding

a new python example for a new learner to educate from

Uploaded by

marwanramzy34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Coding

a new python example for a new learner to educate from

Uploaded by

marwanramzy34
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Certainly! Here's a simple implementation of outdated.

py that prompts the user for a date in


either MM/DD/YYYY or Month DD, YYYY format and then outputs the date in YYYY-MM-DD format.
The program continues to prompt the user until a valid date is provided.

python
Copy code
import datetime

def convert_date(date_str):
months = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
]

# Try to parse date in MM/DD/YYYY format


try:
date_obj = datetime.datetime.strptime(date_str, "%m/%d/%Y")
return date_obj.strftime("%Y-%m-%d")
except ValueError:
pass

# Try to parse date in Month DD, YYYY format


for month in months:
if date_str.startswith(month):
try:
date_obj = datetime.datetime.strptime(date_str, f"{month} %d,
%Y")
return date_obj.strftime("%Y-%m-%d")
except ValueError:
pass

return None

def main():
while True:
date_str = input("Enter a date (MM/DD/YYYY or Month DD, YYYY): ")
formatted_date = convert_date(date_str)
if formatted_date:
print(f"Formatted date: {formatted_date}")
break
else:
print("Invalid date format. Please try again.")

if __name__ == "__main__":
main()

Explanation:

1. Importing datetime Module: The datetime module is imported to help parse and
format dates.
2. convert_date Function:
o This function tries to parse the input date string in two formats:
 MM/DD/YYYY
 Month DD, YYYY
o It uses datetime.datetime.strptime to attempt to convert the input string to a
datetime object.
o If successful, it formats the date as YYYY-MM-DD and returns it.
o If parsing fails for both formats, the function returns None.
3. main Function:
o The main function runs an infinite loop that prompts the user to enter a date.
o It calls convert_date to format the date.
o If a valid date is provided, it prints the formatted date and breaks out of the loop.
o If the input is invalid, it prompts the user to try again.

Running the Program:

To run the program, save the code in a file named outdated.py, and execute it using a Python
interpreter. Here’s how you can run it in a terminal:

sh
Copy code
python outdated.py

Example Usage:
css
Copy code
Enter a date (MM/DD/YYYY or Month DD, YYYY): September 8, 1636
Formatted date: 1636-09-08
css
Copy code
Enter a date (MM/DD/YYYY or Month DD, YYYY): 9/8/1636
Formatted date: 1636-09-08

This output indicates that the program correctly converts and formats the input dates.

4o

You might also like