Large functions aren’t very usable or maintainable. It makes a lot of sense to break down the logic of a program into smaller functions that do one thing each. The program can then call these functions in sequence to perform the work.
Write a program ON PYTHON that prompts for a first name, last name, employee ID, and ZIP code. Ensure that the input is valid according to these rules:
• The first name must be filled in.
• The last name must be filled in.
• The first and last names must be at least two characters long.
• An employee ID is in the format AA-1234. So, two letters, a hyphen, and four numbers.
• The ZIP code must be a number.
Display appropriate error messages on incorrect data.
Example Output
Enter the first name: J
Enter the last name:
Enter the ZIP code: ABCDE
Enter an employee ID: A12-1234
"J" is not a valid first name. It is too short.
The last name must be filled in.
The ZIP code must be numeric.
A12-1234 is not a valid ID.
Or
Enter the first name: Jimmy
Enter the last name: James
Enter the ZIP code: 55555
Enter an employee ID: TK-421
There were no errors found.
Constraints
• Create a function for each type of validation you need to write:
validatename
validatezip
validateid
Each of these functions must take a string containing the value to validate. These functions must return a string describing the result.
CODE:
def validateName(name, part):
if not(name.isalpha()):
return ('The ' + part + ' must contain only letters a-z or
A-Z')
if len(name)<1:
return ('The ' + part + ' must be filled in.')
elif len(name)<2:
return (name + ' is not a valid first name. It is too
short.')
else:
return 0
def validateZip(zipcode):
try:
int(zipcode)
return 0
except:
return ('ZIP code must be numeric')
def validateEid(empid):
try:
int(empid[3:])
if (empid[0] in range(0-9) or empid[1] in range(0-9) or empid[2] !=
'-' or len(empid[3:])!=4):
return (empid + ' is not a valid ID')
else:
return 0
except:
return (empid + ' is not a valid ID')
def validateBiodata(biodata):
errors = []
count=0
errors.append(validateName(biodata['first_name'], 'first'))
errors.append(validateName(biodata['last_name'], 'last'))
errors.append(validateZip(biodata['zipcode']))
errors.append(validateEid(biodata['empid']))
for i in errors:
if i!=0:
print(i)
else:
count += 1
if count==0:
print('No errors found.')
bio = {
'first_name' : input('Enter first name '),
'last_name' : input('Enter last name '),
'zipcode' : input('Enter zip code '),
'empid' : input('Enter employee id ')
}
validateBiodata(bio)
def validateName (name, part): if len(name)<1: return ('The ' + part + ' must be filled in.') elif len(name) <2: return (name + 'is not a valid first name. It is too short.'), else: return def validateZip(zipcode): try: int(zipcode) return @ except: return ("ZIP code must be numeric'), - def validateEid(empid): try: int(empid[3:]) if (empid[@] in range(0-9) or empid[1] in range(0-9) or empid[2] != '-' or len(empid[3:])!=4): return (empid + ' is not a valid ID'), else: return except: return (empid + ' is not a valid ID')
(validatezime, biodatal larst_name'] def validateBiodata(biodata): errors = [] count=0 errors.append(validateName (biodata['first_name'], 'first')) errors.append(validateName (biodata['last_name'], 'last')); errors.append(validateZip(biodata['zipcode'])) errors.append(validateEid(biodata['empid'])), for i in errors: if i !=0: print(i) else: count += 1 if count==0: _print('No errors found.'); - bio = { "first_name' : input('Enter first name '), "last_name' : input('Enter last name'), "zipcode' : input('Enter zip code '), ''empid' : input('Enter employee id '); validateBiodata(bio)
Enter first name jack Enter last name jill Enter zip code 1234 Enter employee id aa123 aa123 is not a valid ID ... Program finished with exit code o Press ENTER to exit console.
Get Answers For Free
Most questions answered within 1 hours.