【问题标题】:Python function (ifs and inputs) stops partway through, no errorPython 函数(ifs 和输入)中途停止,没有错误
【发布时间】:2020-07-29 21:35:03
【问题描述】:

我正在使用一系列输入和 if 语句来创建一个文本游戏/选择您自己的冒险,其中输入决定了函数中接下来会发生什么。 我正在测试一个函数的一部分,并且应该总共打印四个字符串并带有输入提示,但是在前两个字符串之后它只是移动到函数之后的单元格上。没有错误信息。我正在使用带有最新版本 Python 的 Jupyter Notebook。任何有助于使完整功能运行的帮助。 (请忽略愚蠢的文字,对不起,这是我的第一个问题)

start = input('Welcome to Witness Protection, enter HELP if you need help')
def helper():
if start == 'HELP':
    answer= input('')
    if answer == 'PICK':
        answer= input('')
    elif answer == 'WALK':
        print('')
        if answer == 'TRY':
            answer= input('')
        elif answer == 'WALK AWAY':
            print('')
            if answer == 'IN':
                answer = input('')
            elif answer == 'PUT':
                print('')
                if answer == 'ON':
                    answer = input('')
                elif answer == 'BACK':
                    print('')
if start == 'HELP':
helper()

我检查了我使用的输入是否正确,将 elifs 更改为 ifs 没有想到其他任何可能是问题的任何帮助表示赞赏

【问题讨论】:

    标签: python function if-statement input


    【解决方案1】:

    您似乎想要另一个 input,而不是 elif answer == 'WALK': 之后的 print。当您点击if answer == 'TRY' 时,您还没有给他们机会更改answer 的值。

    此外,您可能需要一种不同的代码结构。当你使用if 时,只有在if 之后和elifelse 之前在同一缩进级别的代码才会运行。这意味着如果有人回答 PICK,那么 WALK 下面的代码都不会运行,因为 elif answer == 'WALK' 部分永远不会被输入。您可能想尝试一个while 循环,其代码检查不同的变量以确定在每个循环中打印什么(例如他们在什么房间,他们有什么物品等),然后从用户那里获得一个新的输入在每个循环结束时。

    【讨论】:

    • 谢谢!我想我最初给每个输入一个不同的名称,我认为我通过让它们都回答来改进代码,这在这里不起作用。感谢结构和 while 循环的建议,它肯定会改进代码,特别是当它变得更复杂时。再次感谢!
    【解决方案2】:

    似乎您与嵌套的 if 有一些不匹配。 你应该根据问题的流向缩进 if ,类似这样:

    def helper():
    if start == 'HELP':
        answer= input('Woah. As you are walking to the FBI office, you see a glistening penny on the floor. Something about it looks strange. What do you do? Enter PICK to pick it up or WALK to keep walking')
        if answer == 'PICK':
            answer= input('You reach down and grasp the penny, and try to pull it. It doesn’t move. Enter TRY to try again or WALK AWAY to walk away')
            if answer == 'WALK AWAY':
                print('You keep walking until you reach the FBI office. You make your way to the office of your agent, and sit down to wait for them.')
            elif answer == 'TRY':
                answer= input('The penny clicks out of place and slides along a track between the paving slabs. One of the slabs slides open. Enter IN to climb in or PUT to put the penny back where it was')
                if answer == 'IN':
                    answer = input('A few metres down, you hit the floor, and see the opening above you close up. You find yourself in an ice cavern, surrounded by the bodies of slain ice dwarfs. Enter ON to walk on or BACK to back')
                    if answer == 'ON':
                        answer = input('You enter the realm of the evil wizard. He tells you he is thinking of giving up evil and asks you if you would like to join him in taking over the world and establishing a utopia. Enter YES for  \'Of course I will join you, let’s do this!\' Enter THINK for \'That’s a big decision, I need some time to think about it\' Enter NO for  \'Woah, sorry buddy, I’m just lost, I’m gonna have to bounce\'')
                    elif answer == 'BACK':
                        print('You scramble back to the surface and try to forget what just happened. You continue towards FBI HQ, and wait for your agent at their desk')
                elif answer == 'PUT':
                    print('You move the penny back to where it was, and the slab slides back into place. You continue your walk towards the FBI offices, and wait for your agent in front of their desk')
        elif answer == 'WALK':
            print('You enter the building and make your way to the office of your agent, and sit down to wait for them.')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      相关资源
      最近更新 更多