Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尝试检测三个不同的输入_Python_Input_Detection - Fatal编程技术网

Python 尝试检测三个不同的输入

Python 尝试检测三个不同的输入,python,input,detection,Python,Input,Detection,我需要创建一个程序来检测输入是否是字符以外的任何东西,以及它是否不会破坏程序。这就是我到目前为止所做的: name = input("Please enter a name: ") while True: try: if name == "Mitnick": print("Most Wanted") break else: print(name,"not on the Most Wa

我需要创建一个程序来检测输入是否是字符以外的任何东西,以及它是否不会破坏程序。这就是我到目前为止所做的:

name = input("Please enter a name: ")

while True:
    try:
        if name == "Mitnick":
            print("Most Wanted")
            break
        else:
            print(name,"not on the Most Wanted List")
            break
    except ValueError:
        print("You didn't enter a name")
        break
即使我输入了一个数字或非字母,它也不会转到ValueError,它总是转到else。我需要找到一种方法让它检测出它是否是一个角色


谢谢

这是因为没有引发异常。
引发异常的原因可能是内置异常触发异常,也可能是您手动触发异常。

您需要使用
isalpha
方法,如果字符串中的所有字符都是字母,该方法将返回True

name = input("Please enter a name: ")

while True:
    try:
        if name == "Mitnick":
            print("Most Wanted")
            break
        elif name.isalpha():
            print(name,"not on the Most Wanted List")
            break
        else:
            raise ValueError
    except ValueError:
        print("You didn't enter a name")
        break

您可以使用
name.isalpha()
检查它是否都是字符。那么我该怎么做呢?我不知道isalpha是一个命令。这正是我想要的,非常感谢!