Advertisement
timber101

DOBEntryValidator

Jan 13th, 2021
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. from datetime import datetime
  2.  
  3. # validating year is an integer within an expected age range
  4. inpyear = input("please enter a year: >> ")
  5. inpyearIsValid = False
  6.  
  7. while not inpyearIsValid:
  8.   try:
  9.     inpyear = int(inpyear)
  10.     if inpyear >(2021-117) and inpyear <2021:
  11.       print(f"Thats a valid entry, {inpyear}")
  12.       inpyearIsValid = True
  13.     else:
  14.       inpyear = (f"That entry{inpyear} is outside expectation, try again >>")
  15.   except ValueError:
  16.     inpyear = input(f"{inpyear} is Invalid. Enter a year > ")
  17.  
  18.  
  19. # validating month has been entered and changing to title case and list of months
  20. inpmonth = input("please enter month: >> ")
  21. inpmnthIsValid = False
  22.  
  23. while not inpmnthIsValid:
  24.   months_of_year=["January","February","March","April","May","June","July","August","September", "October","November","December"]
  25.   if inpmonth.title() in months_of_year:
  26.     print(f"{inpmonth} is a valid input")
  27.     inpmnthIsValid = True
  28.   else:
  29.     print(f"youe entered {inpmonth} which is invalid, please try again")
  30.     inpmonth = input("please enter month: >> ")
  31.  
  32.  
  33. #validating entry is an integer and then valid for month
  34. inpday = input("please input a day in month >> ")
  35. inpdayIsValid = False
  36.  
  37. while not inpdayIsValid:
  38.   try:
  39.     inpday = int(inpday)
  40.     try:
  41.       datestr= (f"{inpday}-{inpmonth}-{inpyear}")
  42.       dt_obj = datetime.strptime(datestr, "%d-%B-%Y")
  43.       print(f"{datestr} is a valid date")
  44.       inpdayIsValid = True
  45.     except:
  46.       print(f"{datestr} does not exist")
  47.       inpday = input(f"{inpday} is Invalid. Enter a day valid for {inpmonth} in {inpyear}) > ")
  48.   except ValueError:
  49.     inpday = input(f"{inpday} is Invalid. Enter a day (1-31) > ")
  50.  
  51.  
  52.  
  53. """
  54. first attempt with two nested trys gasp
  55. while not inpdayIsValid:
  56.  try:
  57.    inpday = int(inpday)
  58.    if inpday >=1 and inpday <=31:
  59.      print(f"Thats a valid entry, {inpday}")
  60.      datestr= (f"{inpday}-{inpmonth}-{inpyear}")
  61.      print(datestr)
  62.      print(type(datestr))
  63.      try:
  64.        dt_obj = datetime.strptime(datestr, "%d-%B-%Y")
  65.        print(f"{datestr} is a valid date")
  66.        inpdayIsValid = True
  67.      except:
  68.        print(f"{datestr} does not exist")
  69.        inpday = input(f"{inpday} is Invalid. Enter a day valid for {inpmonth} in {inpyear}) > ")
  70.    else:
  71.        print("out of range")
  72.        inpday = input(f"{inpday} is Invalidz. Enter a day (1-31) > ")
  73.  except ValueError:
  74.    inpday = input(f"{inpday} is Invalid. Enter a day (1-31) > ")
  75. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement