Python 3.x Try/例外,返回到Try

Python 3.x Try/例外,返回到Try,python-3.x,Python 3.x,我有这个密码 def ID(): ID = input("Enter ID: ") try: int(ID) print("Good ID") except ValueError: print("Not a string") ID() 如何在不重新启动代码的情况下再次尝试输入ID?您可以使用while循环。有几种方法。这里有一个: def ID(): goodID = False while not

我有这个密码

def ID():
    ID = input("Enter ID: ")

    try:
        int(ID)
        print("Good ID")
    except ValueError:
        print("Not a string")


ID()

如何在不重新启动代码的情况下再次尝试输入ID?

您可以使用while循环。有几种方法。这里有一个:

def ID():
    goodID = False
    while not goodID:
        ID = input("Enter ID: ")
        try:
            int(ID)
            print("Good ID")
            goodID = True
        except ValueError:
            print("Not a string")  # do you mean int??

ID()

您可以使用while循环。有几种方法。这里有一个:

def ID():
    goodID = False
    while not goodID:
        ID = input("Enter ID: ")
        try:
            int(ID)
            print("Good ID")
            goodID = True
        except ValueError:
            print("Not a string")  # do you mean int??

ID()