如何将空输入计算为 else

3

我是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 2(这是为大学作业而准备的)。现在它已经可以工作了,非常感谢!

3个回答

0

如果您正在使用Python 2,则更改为:

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

to:

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

raw_input 返回一个字符串,而 input 返回一个 整数。这在 Python 3 中已经改变了,现在只有 input 并且它返回一个字符串。

所以如果你正在使用 Python 2,请将其更改为 raw_input,然后像这样调用其他条件:

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

谢谢,这确实帮了很多。我将输入更改为raw_input,因为我确实在使用Python 2(.7)。但是当我添加int部分时,程序出现了问题,所以我只是将数字放在引号中,现在一切都很顺利。非常感谢:D - GOAT
是的,你也可以这样做。我很高兴能帮到你 :) 祝你在项目的其余部分好运! - Serial

0

将最后一个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上测试过? - Ray Toal
@RayToal Python 3。 在Python 2中,您需要将“input”函数更改为“raw_input”,然后“ans”更改为“int(ans)”。 - Anshul Goyal
@GOAT 你可能也有Python 3。尝试在终端中执行 python3 --version - Anshul Goyal

0

使用raw_input代替inputinput会尝试评估输入,评估空字符串会引发错误。raw_input不进行评估,只是提供一个字符串。请参见此说明

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()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接