0

#input of october/9/1701 results in reprompt expected program to reject input

#input of September 8 1636 results in reprompt expected program to reject input

its suppose to reject both input but it keeps reprompting

def main():
    outdate()

def outdate():
    
    months =[

        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
        ]
    month = day = year = ""    

    while True:
        date = input("Date: ").strip()
        if "/" in date:
            month, day, year = date.replace(",", " ").split(" ")
        else:
            month, day, year = date.replace(",", "").split()   
            if month in months:
                month = months.index(month) + 1
            else:
                continue
        try:
            day = int(day)
            year = int(year)
            month = int(month)
            if day > 31 or month > 12:
                continue
        except ValueError:
            pass
        
        break

    print(f"{year}-{month:02}-{day:02}") 

if __name__ == "__main__":

    main()
  • Does this answer your question? [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) – mkrieger1 Oct 02 '22 at 23:29

1 Answers1

0

First off, it's pretty unclear what constitutes as valid/invalid dates. Please do correct me if I'm wrong, but I'm assuming that your check is simply for the given dates:

  • October 9 1701
  • September 8 1636

In this case, if you already have a month/date/year variables, then you can declare a "validity" checker as such:

def validity_checker(m, d, y):
    
    if (d == 8 and m == "September" and y == 1636):
        return False

    if (d == 9 and m == "October" and y == 1701):
        return False
    
    return True

You can then call it in your main function, like this:


def main():
    // other code
    if (not validity_checker(m, d, y)):
        return
    //other code

Does this help?

  • those dates are just part of a bunch of dates that i need to check to complete the project, so its suppose to invalidate those dates and not repromt me for them, so write a function to invalidate those 2 days alone is not it – Adebayo Yusuf Oct 03 '22 at 00:37