try this… you need to to modify the original code to accept a date input in the format “YYYY-MM-DD”, you can use the strptime method from the datetime module
you should also add exceptions for dumb users not following instructions such as inputting dat-month-year or using slashes “/” instead of dash “-”
try this:
from datetime import datetime
from dateutil import relativedelta
def parse_date(date_string):
# This function takes a date string in the format "YYYY-MM-DD" and returns a date object.
try:
parsed_date = datetime.strptime(date_string, "%Y-%m-%d").date()
# Check if the input has the correct date format (year, month, day) and the correct separator
if date_string == parsed_date.strftime("%Y-%m-%d") and "-" in date_string:
return parsed_date
else:
raise ValueError
except ValueError:
print("Invalid date format. Please use the correct dash format: YYYY-MM-DD.")
return None
print("Du kan kolla när du ska ha service!")
# Parse the initial date string and calculate the next check date
d = parse_date("2023-04-20")
if d is not None:
ny = d + relativedelta.relativedelta(years=1)
print()
print(f"Sista check: {d}")
print(f"Next check: {ny}")
# Ask the user for their last check date
user_date_input = input("Enter the date of your last check (YYYY-MM-DD): ")
user_date = parse_date(user_date_input)
# If the input is valid, calculate and print the next check date
if user_date is not None:
next_check = user_date + relativedelta.relativedelta(years=1)
print()
print(f"Sista check: {user_date}")
print(f"Next check: {next_check}")
else:
print("Invalid input. Unable to calculate the next check date.")
you may need to run it from shell as the console will interpret certain things like slashes badly.
Just an old coder from the assembler/ cobol/fortran /c days. We always had to assume dumb users back then because if you didn’t really bad things happened.
we had a saying
“to Err is human, to really F&^K things up requires a computer and user interaction”