Python Forum
struggling with != statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
struggling with != statements
#1
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
Reply
#2
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.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Struggling with Juggling JSON Data SamWatt 7 1,887 May-09-2022, 02:49 AM
Last Post: snippsat
  Syntax errors: Struggling to setup enviroment and load packages AH56 5 2,783 Jun-30-2021, 01:01 PM
Last Post: AH56
  Struggling for the past hour to define function and call it back godlyredwall 2 2,219 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  I’m Flat out struggling to understand list indexes gr3yali3n 7 2,916 Jul-20-2020, 07:18 PM
Last Post: princetonits
  Struggling with nested list gr3yali3n 3 2,311 Jul-09-2020, 05:30 PM
Last Post: DPaul
  Struggling to exit this while loop fatherted99 5 2,476 Feb-08-2020, 07:46 PM
Last Post: fatherted99
  Struggling with several while loops nsadams87xx 1 1,823 Nov-25-2019, 02:12 AM
Last Post: Larz60+
  struggling with loop/webscrape zarize 4 2,488 Sep-27-2019, 08:44 AM
Last Post: zarize
  Still struggling with np.where... pberrett 1 1,847 May-10-2019, 11:30 AM
Last Post: scidam
  Struggling To Work With Anti-Captcha API digitalmatic7 1 5,713 Oct-09-2017, 09:51 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020