为什么双位数选择不能在python中返回正确的响应?

Ste*_*ven 0 python task

我正在学习如何使用python.在了解我不理解的定义时发现了一个问题.我给出一个简单的菜单,选择0-4.如果用户选择4以上,则应该收到一条消息"这不是一个有效的选择..."

但是,如果您输入的值大于或等于10,则除菜单外不会返回任何内容...没有消息.

在此先感谢任何想法.

这是我的代码:

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

def exit():
    print("See You Later!")
def task1():
    print("This is Task 1!")
def task2():
    print("This is Task 2!")
def task3():
    print("This is Task 3!")
def task4():
    print("This is Task 4!")

choice = None
while choice != "0":
    print(
        """
        Multitask Selector

        0 - Quit
        1 - Task 1
        2 - Task 2
        3 - Task 3
        4 - Task 4
        """
        )

    choice = input("Pick a Task Between 1-4:\t#")
    print()

    # Exit
    if choice == "0":
        exit()

    # Task 1
    elif choice == "1":
        task1()

    # Task 2
    elif choice == "2":
        task2()

    # Task 3
    elif choice == "3":
        task3()

    # Task 4
    elif choice == "4":
        task4()

    # Not a Correct Selection
    elif choice > "4":
        print("That is not a valid choice.  Please Select a Task Between 1-4.")
Run Code Online (Sandbox Code Playgroud)

DSM*_*DSM 7

你正在比较选择,这是一个字符串(我假设你的打印函数中的Python 3),"4",也是一个字符串.

elif choice > "4":
Run Code Online (Sandbox Code Playgroud)

这按字典顺序排列:

>>> '1' < '2'
True
>>> '1' < '100'
True
>>> '100' < '2'
True
Run Code Online (Sandbox Code Playgroud)

如果你想要数字比较,你必须把选择变成一个数字,例如

>>> int('1') > 4
False
>>> int('10') > 4
True
>>> 
Run Code Online (Sandbox Code Playgroud)