0

I am making a small program that allows a user to enter a flight specification and it will calculate the price. I have implemented a small error catcher, however this error catcher is always true for some reason:

import time 

def main():

    AMS_DESTINATION = "Schipol, Amsterdam"
    GLA_DESTINATION = "Glasgow, Scotland"
    AMS_PRICE = 150.00
    GLA_PRICE = 80.00

    
    flightSpecification = str(input("Please enter your flight specification: "))
    
    flightDestination = flightSpecification[0:3]
    print(flightDestination)

    if flightDestination.lower() != 'ams' or 'gla': # This if statement is always true // Why??
        print("Please enter a valid flight specification!")
        time.sleep(2)
        main()

    def flightCalculations():
  
        if flightDestination.lower() == 'ams':
            userDestination = AMS_DESTINATION

        if flightDestination.lower() == 'gla':
            userDestination = GLA_DESTINATION

main()

I just need it to completely ignore this if the first three letters of 'flightSpecification' are equal to 'ams' or 'gla'. Thanks.

csPYTHONcs
  • 187
  • 1
  • 8
  • 1
    You want `if flightDestination.lower() != 'ams' and flightDestination.lower() != 'gla'` – CryptoFool Oct 22 '20 at 16:04
  • 1
    the if logic doesnt work the way you think it does. Your essentially saying `if (flightDestination.lower() != 'ams') or 'gla'` so your checking if flightdest is not equal to ams. if thats not true then you are checking if 'gla' is true. any non empty string will always return true – Chris Doyle Oct 22 '20 at 16:04
  • 1
    I'm not sure but when you say `a.lower() != 'foo' or 'bar'` the interpreter checks `a.lower() != 'foo'` or `'bar'` and the second one is always `True`. – Sam Ebison Oct 22 '20 at 16:05
  • 1
    `if flightDestination.lower() not in ['ams', 'gla']` – SiHa Oct 22 '20 at 16:06
  • 1
    also if want to check if a string *starts with* a specific substring, you need to use [`.startswith`](https://docs.python.org/3/library/stdtypes.html#str.startswith) on the string. `==` and `!=` are equality checks - they only return `True`/`False` by comparing the entire string. It seems you're not looking to compare the entire string but only the first 3 letters (aka what it starts with) – Chase Oct 22 '20 at 16:08
  • 1
    And BTW it's "Schip**h**ol". – Klaus D. Oct 22 '20 at 16:25

2 Answers2

2

You meant to do this

if flightDestination.lower() not in ['ams', 'gla']:
    # ...

You did

if flightDestination.lower() != 'ams' or 'gla'

which breaks down to

if (condition1) or (condition2)

with condition 1 being flightDestination.lower() != 'ams' and condition 2 being 'gla'.

Condition 2 is a non-empty string, so it's always True. And

if (anything) or True: 

is always True.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    The simplest fix would be to turn `if flightDestination.lower() != 'ams' or 'gla'` into `if flightDestination.lower() != 'ams' or flightDestination.lower() != 'gla'` – Random Davis Oct 22 '20 at 16:06
  • 2
    @RandomDavis Arguably `undesirable_value not in list_of_undesirable_values` is the simplest fix. Repeating the condition quickly gets unwieldy as the list of undesirable values gets longer. – Tomalak Oct 22 '20 at 16:08
  • 1
    I agree, still it's confusing to a beginner why their code didn't work, it's probably better for them to at least understand how to do what they were trying to do, and then afterward be shown a better way. – Random Davis Oct 22 '20 at 16:09
1

The flightCalculations() haven't been called anywhere. And also function changed variable in local scope. So add a return user Destination line at the end of function. Also call the function after the print statement which shows result of flightDestinations like, Ret = flightCalculations()

Wasif
  • 14,755
  • 3
  • 14
  • 34