-2
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')

这段代码根本不起作用。当我在电影标题中加入 Mockingjay 或 mockingjay 以外的其他内容时,效果很好,但如果我放入这些内容,它仍然会说这部电影没有放映。什么时候应该继续说有多少免费座位以及证书是什么。有任何想法吗?我使用 Python 3.1。

4

1 回答 1

4
if film != 'Mockingjay' or 'mockingjay':

需要是

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

原因or 'mockingjay'总是这样True。( bool('mockingjay') = True)

使用film.upper()有助于确保无论输入大小写如何,例如:mocKinjay,MOCKINgjaymockingjay,它都会始终捕获。

如果要检查 2 个不同的字符串,请使用:

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

请注意and,如果我们or在此处使用并传入Mockingjay该语句仍会计算,因为film != 'mockingjay'.

于 2015-06-08T18:20:27.000 回答