0

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
        
biggboss2019
  • 220
  • 3
  • 8
  • 30
  • 1
    If you expect to loop around and ask for info again, then don't raise an exception. Return a Boolean success/fail code, and use that to `break` out of an otherwise infinite loop. – Tim Roberts Jul 09 '22 at 01:33

1 Answers1

1

Here's an example that shows the basic format. For each input, do an infinite while loop until you are satisfied.

import datetime

def validate(date_text):
    try:
        datetime.datetime.strptime(date_text, '%Y-%m-%d')
        return True
    except ValueError:
        print("Incorrect date format, please enter date in YYYY-MM-DD ONLY")
        return False

name_with_deg=input("Enter name & degree separated by (,):")

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

print(name_with_deg)

Sample session:

[timr@Tims-Pro:~/src]$ python x.py
Enter name & degree separated by (,):a,b
Enter employee start and termination date separated by (,)  in YYYY-MM-DD format only: 2022-01-01,2022-03-03
a,b
[timr@Tims-Pro:~/src]$ 
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30