Mori007

validating input dating

Feb 22nd, 2021
849
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Data the months with range of the days
  2. months = {"January":range(31),"February":range(28),"March":range(31),
  3.             "April":range(30),"May":range(31),"Juni":range(30),
  4.             "July":range(31),"August":range(31),"September":range(30),
  5.             "October":range(31),"November":range(30),"December":range(31)}
  6.  
  7. print("Please enter the year:")
  8. year = input("Year.. 1900 - 2021 > ")
  9.  
  10. # Validating a year
  11. while year not in range(1900,2021):
  12.     try:
  13.         year = int(year)
  14.         if year not in range(1900,2021):
  15.             print("Please enter a valid year. (1900-2020)")
  16.             year = input("> ")
  17.         else:
  18.             # Considering February
  19.             if year % 4 == 0:
  20.                 months["February"] = range(29)
  21.     except:
  22.         print("Please enter a valid year. (1900-2021)")
  23.         year = input("again > ")
  24.        
  25.  
  26. print("Enter your Month")
  27. month = input("Month.. January - December > ")
  28.  
  29. # Validating the months
  30. while month not in months:
  31.     month = month.capitalize()
  32.     if month not in months:
  33.         print("Please enter a valid month :")
  34.         month = input("Month  > ")
  35.  
  36.  
  37. print("Enter your day")
  38. day = input(" Day > ")
  39.  
  40. # Validating the day
  41. while True:
  42.     try:
  43.         day = int(day)
  44.         if day-1 not in months[month]:
  45.             print("Please enter a valid day.")
  46.             day = input("> ")
  47.             continue
  48.         else:
  49.             break
  50.     except:
  51.         print("Please enter a valid day!")
  52.         day = input("> ")
  53.  
  54.  
  55. print(f"The date is {day} - {month} - {year}")
  56.  
RAW Paste Data