Scenario
You are writing a computer program for an airline company. The program will be used at a check-in desk to generate and print custom boarding passes.
The program captures several user inputs before generating the pass.
In order to make your program more robust, you decide to implement validation routines so that any invalid input is detected and automatically rejected by the program.
Your task is to complete the code below to implement the following validation checks:
- The firstname and lastname of the passenger cannot be left blank,
- The airport codes (departure and arrival) have to be exactly 3 characters long,
- The airport codes (departure and arrival) should be automatically converted to UPPERCASE,
- The program should ask whether or not a QR code will be printed on the boarding pass. Only a “Yes” or a “No” answer should be accepted,
- The gate number has to be based on the following format: 1 uppercase letter + 2-digit number,
- The flight number has to be based on the following format: 2 uppercase letters + 4-digit number,
- The departure and arrival times must be in the 12:60 AM/PM format,
- The date must be in the DD/MM/YYYY format.
Validation Techniques
String CasePresence CheckInteger OnlyRange CheckLookup CheckCharacter CheckLength CheckTry again!
In Python you can change the case of a sting using .lower(), .upper() and .title() method.
e.g. To retrieve a username in lower case:
username = input("Enter your username:").lower()
e.g. To retrieve a last name in title case:
lastname = input("Enter your last name:").title()
e.g. To retrieve a postcode in upper case:
postcode = input("Enter your postcode:").upper()
You can also remove all leading and tailing space from a user input you can use the .strip() method.
name = input("Enter your name:").strip()
Input Validation: Presence Check
name = input("Enter your name:").strip() if name=="": print("Empty name!") else: print("Thank you!")
Input Validation: Type Check – Integer?
number = input("Type a number:") if number.isdigit(): print("This is a number") else: print("This is not a whole number")
Input Validation: Range Check
number = int(input("Type a number between 1 and 5:")) if number>=1 and number<=5: print("Valid number") else: print("Invalid number")
Input Validation: Lookup Check
drive = input("Can you drive?").lower() if drive in ["yes","no"]: print("Valid answer") else: print("Invalid answer")
Input Validation: Character Check
email = input("Type your e-mail address:") if "@" in email: print("Valid e-mail address") else: print("Invalid e-mail address")
postcode = input("Type your postocode:").upper() if postcode[0] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and postcode[1] in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" and postcode[2] in "123456789": print("Valid postcode") else: print("Invalid postcode")
Input Validation: Length Check
password = input("Type a password:") if len(password)>=8: print("Valid password") else: print("Invalid password")
Try Again! Using a While Loop:
name = input("Enter your name:") while name=="": print("You must enter your name! Please try again!") name = input("Enter your name:") print("Welcome " + name)
You can investigate more advance approaches to implement validation subroutines on this blog post.
Complete the code
Solution...
The solution for this challenge is available to full members!Find out how to become a member:
➤ Members' Area