Python Forum
How to get program to loop if input date is in the past? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: How to get program to loop if input date is in the past? (/thread-43368.html)



How to get program to loop if input date is in the past? - xander_thornton45 - Oct-10-2024

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:
    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?


RE: How to get program to loop if input date is in the past? - tekberg - Oct-11-2024

The problem is that the else clause in the try / except / else block will *always* return. You should change that else clause to test 'flag' and return if it is false. Instead of naming that variable 'flag', how about naming it 'keep_looping'?


RE: How to get program to loop if input date is in the past? - deanhystad - Oct-11-2024

You should only return a date if input_date >= today. Only one return statement, not 2.

Other nitpicks:

There is no need for more than one print statement saying you messed up on the date, unless you want different messages for entering a date in the past and entering a date that is not correctly formatted.

There is no need for the today variable. It is only used once. Just compare the input data against datetime.now()

There is no need for the flag in the while loop. The function returns a correct date. You don't have to worry about ending the loop.

You have a return statement but no function. I assume this is a copy paste error.


RE: How to get program to loop if input date is in the past? - Pedroski55 - Oct-11-2024

Maybe like this:

from datetime import datetime
import re

e = re.compile(r'\d\d/\d\d/\d\d\d\d')
today = datetime.now()
day = datetime.strftime(today, "%d/%m/%Y")
while True:
    user_date = input('Please enter a date > today\'s date when you wish to visit Recoats Adventure Park (DD/MM/YYYY): ')
    # check if the date has the correct format
    if e.match(user_date):
        # check if the rquested date is in the future
        if user_date > day:
            print(f'Fine we have tickets for {user_date} ... ')
            break
        else:
            print("Sorry, the date you enter must be at least tomorrow or later.")
    else:
        print(f'The date you entered, {user_date}, is the wrong format, please try again.')