【问题标题】:How to count empty input as an else如何将空输入计为其他
【发布时间】:2013-10-05 17:03:39
【问题描述】:

我是 python 新手,正在上课。下面是我的代码。我的问题是在我想要的最后一行,这样如果有人简单地按下回车,它就会被视为有人输入了(1/2/3 或 4)以外的输入,这基本上会用最后一个 elif 回复并重新启动问题.但目前它给了我这个:

SyntaxError: unexpected EOF while parsing. 

有人可以帮忙吗?

def menu_loop():
    ans = True
    while ans:
        print("""
        1. My freedom awaits me, give me my blade!
        2. I've never fought before, might you help me?
        3. I have already forsaken freedom, I will fight till the death.
        4. Fighting is for the wicked. I refuse to fight.

        Press enter to quit
        """)

        ans= input("What would you like to do now?")
        if ans == 1:
            main_loop()
            break
        elif ans == 2:
            instructions_loop()
            break
        elif ans == 3:
            gauntlet_loop()
            break
        elif ans == 4:
            print("\nMen... come relieve this man of his life.")
            time.sleep(2)
            end_game_loop()
            break
        elif ans == '': 
            print("\nDo you not speak Latin, slave?")

谢谢,感谢Christian,我找到了答案。我猜我从 Python 3 获得指示的网站是 Python 3,而我在 2(这是为了大学作业)。现在正在运行,非常感谢!

【问题讨论】:

    标签: python python-2.7 input


    【解决方案1】:

    将最后一个 elif 条件改为elif not ans

    def menu_loop():
        ans = True
        while ans:
            print("""
            1. My freedom awaits me, give me my blade!
            2. I've never fought before, might you help me?
            3. I have already forsaken freedom, I will fight till the death.
            4. Fighting is for the wicked. I refuse to fight.
    
            Press enter to quit
            """)
    
            ans = input("What would you like to do now?")
            if ans == 1:
                main_loop()
                break
            elif ans == 2:
                instructions_loop()
                break
            elif ans == 3:
                gauntlet_loop()
                break
            elif ans == 4:
                print("\nMen... come relieve this man of his life.")
                time.sleep(2)
                end_game_loop()
                break
            elif not ans: 
                print("\nDo you not speak Latin, slave?")
    

    编辑:答案是针对 python 3。 对于 python 2.7,使用 raw_input 代替 input,并相应地使用 int(ans) 或字符串文字。

    【讨论】:

    • 您是在 Python 2 上还是仅在 Python 3 上测试过这个?
    • @RayToal python 3. 在python 2上,您需要将input函数更改为raw_input,然后将ans更改为int(ans)
    • @GOAT 你可能也有 python 3。尝试在您的终端中执行python3 --version
    【解决方案2】:

    使用raw_input 而不是inputinput 将尝试评估输入,评估空字符串会引发错误。 raw_input 不评估,只是给你一个字符串。见this explanation

    def main_loop():
        pass
    def instructions_loop():
        pass
    def gauntlet_loop():
        pass
    def end_game_loop():
        pass
    
    next_loop_func = {
        '1': main_loop, 
        '2': instructions_loop, 
        '3': gauntlet_loop,
        '4': end_game_loop,
    }
    def menu_loop():
        while True:
            print("""
            1. My freedom awaits me, give me my blade!
            2. I've never fought before, might you help me?
            3. I have already forsaken freedom, I will fight till the death.
            4. Fighting is for the wicked. I refuse to fight.
    
            Press enter to quit
            """)
    
            ans = raw_input("What would you like to do now?")
            if ans in next_loop_func:
                next_loop_func[ans]
            else: 
                print("\nDo you not speak Latin, slave?")
    
    
    menu_loop()
    

    【讨论】:

      【解决方案3】:

      如果您使用的是 python 2,请更改:

      ans = input("What would you like to do now?")
      

      到:

      ans = raw_input("What would you like to do now?")  
      

      raw_input 返回一个字符串,而输入返回一个 integer 这在 python 3 中已更改,在 3 中只有 input 并且它返回一个字符串

      因此,如果您使用的是 python 2,请将其更改为 raw_input,然后像这样调用您的其他条件:

      if int(ans) == 1:
          main_loop()
          break
      

      【讨论】:

      • 谢谢,这帮助很大。我将输入更改为 raw_input,因为我确实使用的是 python 2(.7)。但是,当我添加 int 部分时,事情就坏了,所以我只是将数字放在引号内,现在一切正常。非常感谢:D
      • 是的,你也可以这样做,我很高兴我能提供帮助 :) 祝你项目的其余部分好运!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多