为什么此检查用户输入内容的代码无法正常工作?

高丽

当我问用户他们想要使用哪个对象的程序时,会忽略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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么此代码无法正常工作

来自分类Dev

为什么此代码无法正常工作?

来自分类Dev

为什么此代码无法正常工作?

来自分类Dev

为什么此轮廓检测代码无法正常工作?

来自分类Dev

为什么此jQuery chatbot的代码无法正常工作?

来自分类Dev

为什么代码提示用户输入内容,但不等待而退出呢?

来自分类Dev

为什么funciton gets无法正常工作并跳过用户输入?

来自分类Dev

为什么我的密码检查代码无法正常工作?无论输入什么,它都能提供相同的输出

来自分类Dev

为什么此代码正常工作?

来自分类Dev

为什么我无法获得密码类型的输入内容

来自分类Dev

为什么此cronjob无法正常工作?

来自分类Dev

为什么此KeyEvent无法正常工作?

来自分类Dev

为什么此onClick无法正常工作?

来自分类Dev

为什么此.innerHTML无法正常工作?

来自分类Dev

为什么此程序无法正常工作?

来自分类Dev

为什么此htaccess无法正常工作

来自分类Dev

为什么此代码无法正常运行?

来自分类Dev

为什么jQuery代码无法正常工作?

来自分类Dev

为什么我的代码无法正常工作?

来自分类Dev

我不知道为什么此MARIE Sim代码无法正常工作

来自分类Dev

循环此数组时,为什么我的代码无法正常工作

来自分类Dev

为什么此代码无法正常工作?不会接受elif语句

来自分类Dev

为什么此Cron工作无法正常工作?

来自分类Dev

为什么此代码实际上可以正常工作?

来自分类Dev

为什么此过滤器无法正常工作?

来自分类常见问题

为什么此合并排序实现无法正常工作?

来自分类Dev

为什么此Python regex否定外观无法正常工作?

来自分类Dev

为什么此after_save无法正常工作?

来自分类Dev

为什么此警报对话框无法正常工作

Related 相关文章

  1. 1

    为什么此代码无法正常工作

  2. 2

    为什么此代码无法正常工作?

  3. 3

    为什么此代码无法正常工作?

  4. 4

    为什么此轮廓检测代码无法正常工作?

  5. 5

    为什么此jQuery chatbot的代码无法正常工作?

  6. 6

    为什么代码提示用户输入内容,但不等待而退出呢?

  7. 7

    为什么funciton gets无法正常工作并跳过用户输入?

  8. 8

    为什么我的密码检查代码无法正常工作?无论输入什么,它都能提供相同的输出

  9. 9

    为什么此代码正常工作?

  10. 10

    为什么我无法获得密码类型的输入内容

  11. 11

    为什么此cronjob无法正常工作?

  12. 12

    为什么此KeyEvent无法正常工作?

  13. 13

    为什么此onClick无法正常工作?

  14. 14

    为什么此.innerHTML无法正常工作?

  15. 15

    为什么此程序无法正常工作?

  16. 16

    为什么此htaccess无法正常工作

  17. 17

    为什么此代码无法正常运行?

  18. 18

    为什么jQuery代码无法正常工作?

  19. 19

    为什么我的代码无法正常工作?

  20. 20

    我不知道为什么此MARIE Sim代码无法正常工作

  21. 21

    循环此数组时,为什么我的代码无法正常工作

  22. 22

    为什么此代码无法正常工作?不会接受elif语句

  23. 23

    为什么此Cron工作无法正常工作?

  24. 24

    为什么此代码实际上可以正常工作?

  25. 25

    为什么此过滤器无法正常工作?

  26. 26

    为什么此合并排序实现无法正常工作?

  27. 27

    为什么此Python regex否定外观无法正常工作?

  28. 28

    为什么此after_save无法正常工作?

  29. 29

    为什么此警报对话框无法正常工作

热门标签

归档