Python Forum
How to get program to loop if input date is in the past?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get program to loop if input date is in the past?
#1
Exclamation 
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?
Reply
#2
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'?
Reply
#3
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.
Reply
#4
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.')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using If Statements Instead of While Loop in Simple Game Program new_coder_231013 5 5,693 Dec-14-2021, 12:23 AM
Last Post: supuflounder
  How to compile following python loop program reinispl 3 2,747 Oct-27-2021, 01:57 PM
Last Post: DeaD_EyE
  Adding string numbers, while loop and exit without input. Jose 11 10,985 Apr-15-2020, 08:34 AM
Last Post: Jose
  RockPaperScissor program while loop malfunction tonyliang19 3 3,815 Apr-03-2020, 11:09 PM
Last Post: SheeppOSU
  Question about running comparisons through loop from input value Sunioj 2 3,175 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  Need to convert a date and price from a list into appropriate date format & currency SilverLeaf90 1 2,532 Mar-23-2019, 09:41 PM
Last Post: Yoriz
  Program that, inside a loop, does multiple things. needs to print in a certain way reidmcleod 1 3,311 Feb-19-2019, 02:35 PM
Last Post: marienbad
  Program: Input the correct color Truman 4 11,386 Jan-19-2018, 11:55 PM
Last Post: Mekire
  While Loop; Can Else Point Back To Initial Input Command? RodNintendeaux 9 8,132 Oct-04-2017, 05:39 AM
Last Post: gruntfutuk
  Help with loop & user input rdDrp 11 13,685 Dec-23-2016, 06:10 AM
Last Post: rdDrp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020