Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- # validating year is an integer within an expected age range
- inpyear = input("please enter a year: >> ")
- inpyearIsValid = False
- while not inpyearIsValid:
- try:
- inpyear = int(inpyear)
- if inpyear >(2021-117) and inpyear <2021:
- print(f"Thats a valid entry, {inpyear}")
- inpyearIsValid = True
- else:
- inpyear = (f"That entry{inpyear} is outside expectation, try again >>")
- except ValueError:
- inpyear = input(f"{inpyear} is Invalid. Enter a year > ")
- # validating month has been entered and changing to title case and list of months
- inpmonth = input("please enter month: >> ")
- inpmnthIsValid = False
- while not inpmnthIsValid:
- months_of_year=["January","February","March","April","May","June","July","August","September", "October","November","December"]
- if inpmonth.title() in months_of_year:
- print(f"{inpmonth} is a valid input")
- inpmnthIsValid = True
- else:
- print(f"youe entered {inpmonth} which is invalid, please try again")
- inpmonth = input("please enter month: >> ")
- #validating entry is an integer and then valid for month
- inpday = input("please input a day in month >> ")
- inpdayIsValid = False
- while not inpdayIsValid:
- try:
- inpday = int(inpday)
- try:
- datestr= (f"{inpday}-{inpmonth}-{inpyear}")
- dt_obj = datetime.strptime(datestr, "%d-%B-%Y")
- print(f"{datestr} is a valid date")
- inpdayIsValid = True
- except:
- print(f"{datestr} does not exist")
- inpday = input(f"{inpday} is Invalid. Enter a day valid for {inpmonth} in {inpyear}) > ")
- except ValueError:
- inpday = input(f"{inpday} is Invalid. Enter a day (1-31) > ")
- """
- first attempt with two nested trys gasp
- while not inpdayIsValid:
- try:
- inpday = int(inpday)
- if inpday >=1 and inpday <=31:
- print(f"Thats a valid entry, {inpday}")
- datestr= (f"{inpday}-{inpmonth}-{inpyear}")
- print(datestr)
- print(type(datestr))
- try:
- dt_obj = datetime.strptime(datestr, "%d-%B-%Y")
- print(f"{datestr} is a valid date")
- inpdayIsValid = True
- except:
- print(f"{datestr} does not exist")
- inpday = input(f"{inpday} is Invalid. Enter a day valid for {inpmonth} in {inpyear}) > ")
- else:
- print("out of range")
- inpday = input(f"{inpday} is Invalidz. Enter a day (1-31) > ")
- except ValueError:
- inpday = input(f"{inpday} is Invalid. Enter a day (1-31) > ")
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement