27 | Validating Inputs |
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 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 ... |
Get Exercises for Programmers now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.