I am currently taking a course in college, and they have given me an assignment about fixing the code they have given to me.
For this section I have to make it so that the program only allows dates from the future to be input, otherwise it will loop back to the start of the function. However a part the code is currently not working as intended.
This is the aforementioned section of the code:
This is what happens when I input a date that has passed:
For this section I have to make it so that the program only allows dates from the future to be input, otherwise it will loop back to the start of the function. However a part the code is currently not working as intended.
This is the aforementioned section of the code:
while flag: user_date = input('Please enter date you wish to visit Recoats Adventure Park (DD/MM/YYYY): ') today = datetime.now() try: input_date = datetime.strptime(user_date, "%d/%m/%Y") if input_date > today: flag = False else: print("Sorry, you did not enter a valid date") flag = True except: print("Sorry, you did not enter a valid date") flag = True else: return datetime.strptime(user_date, "%d/%m/%Y").date()The program is supposed to loop back to asking for a date if the user's input is invalid. While this works for inputs such as 00/00/0000 (an invalid date), this does not work for dates that have passed.
This is what happens when I input a date that has passed:
Output:Please enter date you wish to visit Recoats Adventure Park (DD/MM/YYYY): 09/09/2022
Sorry, you did not enter a valid date
Please enter the number of adult tickets required:
As you can see, although the program prints out that my input is invalid, the program still continues to the next task. How do I get the code to loop back whenever I do that?