I am currently making a program that takes flight specification data and outputs the flight details to the user (It is a small program just for fun). The specification has to follow a certain format. Example: GLA 2 25 S F (Name of airport, number of bags, age, meal type and seat class), I was wondering if there was a way to validate this and make sure the user enters this format without the program crashing.
Current code:
import time
def main():
AMS_DESTINATION = "Schiphol, Amsterdam"
GLA_DESTINATION = "Glasgow, Scotland"
AMS_PRICE = 150.00
GLA_PRICE = 80.00
flightSpecification = str(input("Enter Flight Specification: "))
flightDestination = flightSpecification[0:3]
bagCount = int(flightSpecification[4])
baggageCost = float((bagCount - 1) * 20)
if flightDestination.lower() != 'ams' and flightDestination.lower() != 'gla':
print("Please enter a valid flight specification! [LLL 0 00 L L]")
time.sleep(2)
main()
if flightDestination.lower() == 'ams':
print(f"Destination: {AMS_DESTINATION}")
print(f"Flight cost: £{AMS_PRICE}")
elif flightDestination.lower() == 'gla':
print(f"Destination: {GLA_DESTINATION}")
print(f"Flight cost: £{GLA_PRICE}")
print(f"Number of bags: {bagCount}")
print(f"Baggage Cost: £{baggageCost}")
main()
Thanks!
-
To validate ICAO codes, one thing I’ve used in the past is to scrape the Wiki page for valid codes, then test the provided code is in the list of valid codes.s3dev– s3dev2020年10月22日 21:45:04 +00:00Commented Oct 22, 2020 at 21:45
2 Answers 2
You can use this pattern:
while True:
flightSpecification = input("Enter Flight Specification: ") # input() already returns a string
flightDestination = flightSpecification[0:3]
if flightDestination.lower() != 'ams' and flightDestination.lower() != 'gla':
print("Please enter a valid flight specification! [LLL 0 00 L L]")
continue # goes back to the beginning of the while loop - skips the break instruction
# add other input validations as needed
break # exits from the while loop
Consider also using exceptions with try/except to make it more pythonic and provide a better developer experience
Comments
You could use a regular expression to do the validation.
[A-Z]{3} \d \d{2} [A-Z] [A-Z] Try it
Explanation:
[A-Z]{3}: Matches three consecutive letters, followed by a single space\d: Matches one digit, followed by a single space\d{2}: Matches two consecutive digits[A-Z]: Matches a single letter
In python, you implement this with the re module
import re
flightSpecification = input("Enter Flight Specification: ") # input already returns a string
if re.match(r"[A-Z]{3} \d \d{2} [A-Z] [A-Z]", flightSpecification, flags=re.IGNORECASE):
# this is a valid flightspec. Do whatever
else:
# Not valid. Do whatever
You could also use capturing groups to extract the relevant portions of the string like so:
([A-Z]{3}) (\d) (\d{2}) ([A-Z]) ([A-Z])
m = re.match(r"([A-Z]{3}) (\d) (\d{2}) ([A-Z]) ([A-Z])", flightSpecification, flags=re.IGNORECASE)
if m:
# valid.
print("airport = ", m.group(1))
print("bags = ", m.group(2))
print("age = ", m.group(3))
print("meal type = ", m.group(4))
print("seat = ", m.group(5))
To force either one of AMS and GLA as the airport, replace the [A-Z]{3} with (AMS|GLA) Try it
If you want to pester your user until they enter a valid input, see Asking the user for input until they give a valid response