python-3.x 用于文本冒险的If语句,该语句要求在继续之前检查元素的列表

xdyibdwo  于 2023-03-31  发布在  Python
关注(0)|答案(2)|浏览(138)

我正在做的一个作业是一个基于Python文本的冒险游戏。它主要由可接受的选择列表和检查这些选择的if语句组成。一个选择需要检查一个名为“inventory”的列表中的元素。如果元素存在,则执行一件事,否则执行其他操作。语句的这一阶段不起作用,只是保持不变,但似乎所有结构都正确。

def right_door():
    print('You push the door on the right open, revealing a long corridor.  "Now it gets interesting..."'
          'You hear a bleeping noise coming from your neck.  "Did you not notice the collar around your neck?  You '
          'may have guessed it already but that is a bomb.  It will blow in exactly three minutes.  Escape the '
          'building and it will disarm.  Otherwise... it\'s bye bye for' + playername + '!!  Oh and to make matters '
          'worse.  I have a friend who wants to meet you.  I would advise you don\'t give him that satisfaction"')
    print('\nYou see a man wielding a chainsaw standing at the door where you came from...')
    print('\n=========================================================================================================')
    print('Got to be quick on your feet here.  What do you do?')
    timer = threading.Timer(180, timer_fail_right_door)
    timer.start()
    choice = input('>> ')
    acceptable_choices = ['run', 'tamper', 'break', 'take of collar', 'escape', 'hide', 'pick up key', 'take key',
                          'key', 'open chest', 'chest', 'open door', 'door', 'escape', 'fight', 'fight him', 'tackle',
                          'tackle']
    while choice.lower() not in acceptable_choices:
        print('I don\'t quite understand that command')

    if choice.lower() in ['run', 'escape']:
        print('You make a break for it, sprinting down the corridor.  You hear heavy footsteps and panting behind you,'
              'driving you to run as quick as your legs will take you.  You come to a large room.  Looks like an old '
              'warehouse or storage facility.  You quickly enter and slam the door behind you!  You see a door '
              'straight in front of you, and there are plenty of places to hide.')
        print('\n=====================================================================================================')
        print('What do you do?')
        choice = input('>> ')
        if choice.lower() in ['hide']:
            print('You duck behind some storage boxes, hoping the person doesnt see you.  They search frantically '
                  'screaming your name.  Whilst you are hiding you spot a key by your feet.')
            print('\n=================================================================================================')
            print('What do you do?')
            choice = input('>> ')
            if choice.lower() in ['pick up key', 'key', 'take key']:
                inventory.append('key')
                print('You take the key.  Perhaps it is for the door ahead of you.  Then again it could also be for '
                      'the treasure chest on the opposite side of the room.')
                print(
                    '\n===============================================================================================')
                print('What do you do?')
                choice = input('>> ')
                if choice.lower() in ['open chest', 'chest']:
                    print('You open the chest.  Your eyes widen as you discover it is full of GOLD!! What you don\'t '
                          'realise is that it is of no use to you here.  You look up and see the bomb straddling mad '
                          'man above you...')
                    print('\nYou have died...')
                    game_over()
                elif choice.lower in ['open door', 'door']:
                    if 'key' in inventory:
                        print('You open the door and step through.  YOU ARE BACK IN YOUR BEDROOM!!  You do not know how'
                              ' you got here or what happened.  You\'re just happy you are home.')
                        game_complete()
                    elif 'key' not in inventory:
                        print('You try and open the door, but it is locked.  You turn round only to be greeted by a '
                              'chainsaw.  You meet a gruesome death.')
                        print('\nYou have dies...')
                        game_over()
zysjyyx4

zysjyyx41#

您在以下行中缺少括号:

elif choice.lower in ['open door', 'door']:

这将不会返回任何内容。它将不会按条件输入。
您需要更新此行,如下所述:

elif choice.lower() in ['open door', 'door']:

而且,我希望您知道,当您在list中检查某些内容时,该命令应该与list元素完全匹配。

6uxekuva

6uxekuva2#

Str.lower是字符串的方法,所以你需要调用函数。使用variable.lower()代替。

相关问题