当输入请求整数时,'ENTER'键在python中带来错误

时间:2012-06-02 18:56:13

标签: python task enter

我正在制作一个菜单系统的模板,我对初学者进行了很好的整理。在过去的几天里,我通过stackoverflow得到了一些很好的帮助,并认为我会问这里有什么问题。

以下是代码:

# Multitasker
# Allows User to Pick an Item that is Defined.

# This is the initial screen.

print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X            DEMO MULTITASK DEMO         X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X               MULTITASKING             X
            X           --------------------         X
            X             MAIN MENU SYSTEM           X
            X            FOR GAME PLATFORMS          X
            X           --------------------         X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

                     PRESS 'ENTER' TO CONTINUE""",end=" ")

# Input used to prevent the Multitask Selector Menu from Appearing at First!
input(" ")

# Defining Task 1 
def task1():
    print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X    YOU HAVE CHOSEN TO DISPLAY TASK 1   X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

# Defining Task 2
def task2():
    print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X    YOU HAVE CHOSEN TO DISPLAY TASK 2   X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

# Defining Task 3
def task3():
    print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X    YOU HAVE CHOSEN TO DISPLAY TASK 3   X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

# Defining Task 4
def task4():
    print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X    YOU HAVE CHOSEN TO DISPLAY TASK 4   X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

# If the Player Selected an innappropriate task number greater than 4, this will   display.
def notatask():
    print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X     THAT IS NOT A POSSIBLE CHOICE.     X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")


# Starts choice off as having no selection.
choice = None
playername = " "

# While the variable 'choice' is not '0', it will continue to display the menu below
while choice != "0":
    print(
        """
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X            MULTITASK SELECTION         X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X                0 - Quit                X
            X                1 - Task 1              X
            X                2 - Task 2              X
            X                3 - Task 3              X
            X                4 - Task 4              X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end=" ")

    choice = int(input("\n\t\tPick a Task Between 0-4:\t#"))
    print()

    # Exit
    if choice == int('0'):
        print("""
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X            DEMO MULTITASK DEMO         X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            X                                        X
            X               MULTITASKING             X
            X           --------------------         X
            X             MAIN MENU SYSTEM           X
            X            FOR GAME PLATFORMS          X
            X           --------------------         X
            X                                        X
            XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
            """, end="")
        print("\t\t", playername, end="")
        print("""
                    PRESS 'ENTER' TO CONTINUE""", end=" ")
        input(" ")

    # Task 1
    elif choice == int('1'):
        task1()

    # Task 2
    elif choice == int('2'):
        task2()

    # Task 3
    elif choice == int('3'):
        task3()

    # Task 4
    elif choice == int('4'):
        task4()

    # Not a Correct Selection
    elif choice > int('4'):
        notatask()

    # Enter Key
    elif choice == ('ENTER'):
        notatask()

所以,我不明白的是当有人点击0,1,2,3或4以外的密钥时,我需要做的就是调用notatask()。

任何人都能引导我朝着正确的方向前进吗?

先谢谢。

最佳,

史蒂芬

2 个答案:

答案 0 :(得分:2)

在任务4的elif之后,只需使用else

# Task 4
elif choice == int('4'):
    task4()

# Everything else
else:
    notatask()

答案 1 :(得分:1)

请勿立即致电int()。首先检查值。像这样:

choice = input("\n\t\tPick a Task Between 0-4:\t#")
if not choice:
    notatask()
    continue
choice = int(choice)