Python Forum

Full Version: struggling with != statements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
def main():
LPL = "LPL"
BOH = "BOH"
print("please enter the option you would like to select:")
print("1:Enter Airport details")
print("2:Enter Flight details")
print("3:Enter price plan and data")
print("4:clear data")
print("5: Quit")
option = input("enter the number of the option you would like to select: ")
if option == '1':
airport = input("Please enter the three letter code of the Uk airport: ")
if airport !=LPL or airport !=BOH:
main()
print("The code you entered was not for a uk airport, you will now be returned to the main menu")
if airport ==LPL or BOH:
overseas = input("Please enter the three letter code of the overseas airport: ")
main()
in the if statements about the airports it always comes back as false and i dont know how to stop this
thanks
It will always be false because even if your input is LPL then it won't be BOH and visa-versa.

One way is to make a list including all the acceptable answers, then check to is if your input is in that list.
Please use code tags (see here) when posting code to make it easier to read.

It actually looks like both of your if conditions would always be True, not False.

if airport !=LPL or airport !=BOH: will always evaluate as True. Because you are using or, Python will check to see if EITHER of your conditions is true. The airport variable can't be equal to both LPL and BOH at the same time, so one of the NOT EQUALS (!=) conditions has to be true and therefore the result is True.

Similarly, if airport ==LPL or BOH is asking "Does airport equal LPL or does BOH exist?". BOH does exist, so regardless of the value of airport this returns True.

You can use in to simplify the code and get what you're looking for:
if airport not in (LPL, BOH):  # returns True if airport is not one of the two specified values, otherwise False

if airport in (LPL, BOH):  # opposite of the above, returns True if airport IS one of the specified values