为什么这段检查用户输入的代码不能正常工作?

Why does this code that checks what input the user puts not work properly?

当我询问用户他们想要使用什么对象时,程序会忽略 elif 语句。

from __future__ import print_function
import random

#Variables

name = raw_input
answer = raw_input
objectList = ['coin', 'piece of paper', 'bundle of sticks']

#Game code

def game():
    name = raw_input("What is your name?: ")
    print ('-----------------------------------------------------------------')
    print ('Hello, ' +  str(name) + ', you tripped into a hole and fell unconcious')
    print ('to wake up in a dungeon. There is a door in front of you, but it ')
    print ('seems to be locked.')
    print ("\n")
    print ('Look around for a tool?')
    askForObject()


#answer for question
def askForObject():
    if "yes" in answer():
        print('-----------------------------------------------------------------')
        print('There are two objects on the ground; a mysterious contraption with')
        print('two prongs, and a ' + random.choice(objectList) + '.')
        print("\n")
        print('Which tool will you pick up? The contraption or the object?')
        pickUpPrompt()               
    else:
        print('What do you mean? Yes, or no?')
        askForObject()

        #ask the user what tool they will pick up
def pickUpPrompt():
    if 'object' in answer():
        print('You picked up the object, but it has no apparent use on the door')
        print('Try again')
        pickUpPrompt()
    elif 'contraption' in answer():
            print('You picked up the contraption.')
            doorPrompt()    
    else:
        print('What object are you talking about?')
        pickUpPrompt()
def doorPrompt():
    print ('-----------------------------------------------------------------')
    print ('Will you use the contraption on the door?')
    if 'yes' in answer():
        print ('door')

game()

我试过使用不同的运算符,但我 运行 遇到了字符串调用方式的问题。如果你输入相同的答案两次似乎有效,但我不知道为什么会这样。

elif 无法正常工作,因为它第二次调用 raw_input。这就是为什么两次输入相同答案有效的原因,因为第二次输入符合 elif 条件。您可以做的是在 pickUpPrompt 的第一行调用 raw_input 并将其保存为变量,然后使用 if/elif/else.

def pickUpPrompt():
    ans = answer()
    if 'object' in ans:
        print('You picked up the object, but it has no apparent use on the door')
        print('Try again')
        pickUpPrompt()
    elif 'contraption' in ans:
            print('You picked up the contraption.')
            doorPrompt()    
    else:
        print('What object are you talking about?')
        pickUpPrompt()