Python - Why is my else statement being ignored in this piece of code?

250 views Asked by At
seats = 3
rating = 12

print('Hello and welcome to Daisy\'s cinema.')

while seats > 0:
    name = input('What is your name?')
    print('Hello',name,'\n')
    print('We are currently showing Mockingjay at out cinema.')
    film = input('What film would you like to watch?')
    if film != 'Mockingjay' or 'mockingjay':
        print('I\'m sorry but we aren\'t showing that film.\n\n')
    else:
        print('This film currently has',seats,'free seats and a certificate of',rating,'\n')
        age = int(input('How old are you?'))
        if age > 15 or age == 15:
            print('You have booked a ticket to see this film.\n\n')
            seats == seats-1
        else:
            print('I\'m sorry but you\'re too young to see this film.\n\n')

print('Mockingjay is now full. Thank you for booking your tickets. \n\n')

This piece of code simply isn't working. When I put in something other than Mockingjay or mockingjay for the film title, it works fine, but if I put these in it still says that this film isn't showing. When it should go on to say how many free seats there are and what the certificate is. Any ideas? I use Python 3.1.

1

There are 1 answers

2
MrAlexBailey On
if film != 'Mockingjay' or 'mockingjay':

Needs to be

if film.upper() != 'MOCKINGJAY':

Reason being that or 'mockingjay' is always True. (bool('mockingjay') = True)

Using film.upper() helps to ensure that regardless of the input case, eg: mocKinjay, MOCKINgjay, mockingjay etc, it will always catch.

If you want to check against 2 different strings then use either:

if film not in ['Mockingjay', 'mockingjay']:
if film != 'mockingjay' and film != 'Mockingjay':

Notice the and, if we use or here and pass in Mockingjay the statement would still evaluate because film != 'mockingjay'.