Advertisement
JAS_Software

Validate Date

May 8th, 2021
52
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. def getInput(text):
  2.     response = input('Enter {}: '.format(text)).strip()
  3.     return response
  4.  
  5. def getMonths():
  6.     months = {
  7.         'january':31,
  8.         'february':28,
  9.         'march':31,
  10.         'april':30,
  11.         'may':31,
  12.         'june':30,
  13.         'july':31,
  14.         'august':31,
  15.         'september':30,
  16.         'october':31,
  17.         'november':30,
  18.         'december':31
  19.         }
  20.     return months
  21.    
  22. def displayInvalid(text):
  23.     print('Invalid {}'.format(text))
  24.  
  25. def validateYear(year):
  26.     valid = False
  27.     try:
  28.         year = int(year)
  29.         valid = (year>= 1900 and year <= 2050)
  30.         if not valid:
  31.             displayInvalid('Year')
  32.     except:
  33.         displayInvalid('Year')
  34.         valid = False
  35.     return valid
  36.  
  37. def validateMonth(month,months):
  38.     valid = False
  39.     valid =  month.lower() in months
  40.     if not valid:
  41.         displayInvalid('Month')
  42.     return valid
  43.  
  44. def validateDay(day,month,months):
  45.     maxDay = months[month]
  46.     valid = (int(day) >= 1 and int(day) <= months[month])        
  47.     return valid
  48.  
  49. okay = False
  50. while not okay:
  51.     year = getInput('Year')
  52.     okay = validateYear(year)
  53.  
  54. months = getMonths()
  55. okay = False
  56. while not okay:
  57.     month = getInput('Month')
  58.     okay = validateMonth(month,months)
  59.  
  60. okay = False
  61. while not okay:
  62.     day = getInput('Day')
  63.     okay = validateDay(day,month,months)
  64.  
  65. print('The date entered is {} {} {}'.format(day,month,year))
  66.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement