I have below python
code. Basically, I am asking multiple inputs from user these are Name, degree, start date and termination date. I want my code to run such that when ask about their name and degree if user enters number then it should throw an error asking them to enter their name and degree in string. Once this requirement is satisfied, it should only then proceed to next input which is start and termination date. Here also I would like to verify that user entered the date in correct format. For second part i.e. for date verification I was able to reference this link. I am not sure about how to proceed with string validation and how to validate the first input and then proceed to next input ?
Thanks in advance!
Code
import datetime
def validate(date_text):
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d')
except ValueError:
raise ValueError("Incorrect date format, please enter date in YYYY-MM-DD ONLY")
name_with_deg=input("Enter name & degree separated by (,):").split(",")
start_date,end_date= input("Enter employee start and termination date separated by (,) in YYYY-MM-DD format only: ").split(",")
print(name_with_deg)
validate(start_date)
validate(end_date)
Update after SO user suggestion
import datetime
def validate(date_text):
try:
datetime.datetime.strptime(date_text, '%Y-%m-%d')
return True
except ValueError:
raise ValueError("Incorrect data format, should be YYYY-MM-DD")
return False
def validate1(string_text):
try:
if(not name_with_deg.isalpha()):
print("Enter name & degree in string only")
return True
except:
raise ValueError("Please enter name and degree in correct format!")
return False
while True:
name_with_deg=input("Enter name & degree separated by (,):").split(",")
if validate1(name_with_deg):
break
while True:
start_date,end_date= input("Enter employee start and termination date separated by (,) in YYYY-MM-DD format only: ").split(",")
if validate(start_date) and validate(end_date):
break