Oct-21-2021, 07:05 PM
I'm looking to combine my logic into a single function to check if the user's input is in my required format and to make sure the date is not in the past. Every time I think about it, it seems like I'm just a step away or there is a very simple solution but I've been stewing over this all day without any success.
Here's what I have so far:
As a bonus, I'm taking the time in a different input so if someone can help me take it as one input, that'd be great too.
Here's what I have so far:
from datetime import datetime user_date = input("Provide date: ") valid = False while not valid: try: date = datetime.strptime( user_date, "%m/%d/%Y").strftime("%m/%d/%Y") valid = True except ValueError: user_date = input("Incorrect date format. Please try again: ")I also have this snippet but am at a lose for how to incorporate it:
past = datetime.strptime(user_date, "%m/%d/%Y") present = datetime.now() if past.date() < present.date(): return True else: return FalseI was thinking I could just put the past date check after the valid format check however if it fails the second check, I need to go back and make sure that their new input is in the valid format again.
As a bonus, I'm taking the time in a different input so if someone can help me take it as one input, that'd be great too.