Question

Large functions aren’t very usable or maintainable. It makes a lot of sense to break down...

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.

Homework Answers

Answer #1

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.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are...
Functions displayGrades and addGrades must be rewritten so that the only parameters they take in are pointers or constant pointers. Directions: Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS = 3; // may only be declared within the main function string students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Be sure to compile using g++ -std=c++11 helpWithGradesPtr.cpp Write a C++ program to run a menu-driven program with the...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT