Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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
在Python3中,如何确定函数参数是否为字母表?_Python_String_Python 3.x - Fatal编程技术网

在Python3中,如何确定函数参数是否为字母表?

在Python3中,如何确定函数参数是否为字母表?,python,string,python-3.x,Python,String,Python 3.x,我在用Python3编写一个简单的程序时被卡住了。代码如下: def check_guess(letter,guess): max = guess.isnumeric() #print(type(max)) print(max) if(max == 'True'): print("Invalid") return False elif(guess > letter): print("High")

我在用Python3编写一个简单的程序时被卡住了。代码如下:

def check_guess(letter,guess):
    max = guess.isnumeric()
    #print(type(max))
    print(max)
    if(max == 'True'):
        print("Invalid")
        return False
    elif(guess > letter):
        print("High")
        return False
    elif(guess < letter):
        print("Low")
        return False
    else:
        print("Correct")
        return True

check_guess("H","2")
程序中没有错误,但我没有得到期望的结果。每当我尝试check_guessH时,2应该返回无效,但它显示为低。如果guess参数不是check_guess函数中的alpha字符,我希望打印无效。我该怎么做?请帮助。

1“正确”与“正确” “True”是一个字符串。True是真正的重言式布尔值。见吉姆的评论

2不要重新定义内置项 首先,不要使用中间变量,也不要将其命名为内置函数。所以,只要这样做:

if not letter.isnumeric():
    print("Invalid")
3.检查正确的论点 你们们说你们们想检查字母是否有效,但你们们确实猜到了


另外,当输入为数字时,是否确实希望它无效?我想你不要信。是数字。。。我想你也确实想检查猜测…

如果你想比较两个字母,你应该用isalpha替换isnumeric:

def check_guess(letter, guess):
    assert isinstance(letter, str)
    assert isinstance(guess, str)
    if not letter.isalpha() or not guess.isalpha():
        print("Invalid")
        return False
    if (guess > letter):
        print("High")
        return False
    elif (guess < letter):
        print("Low")
        return False
    else:
        print("Correct")
        return True

check_guess("H", "2")

您正在检查字符串“True”,而不是True。max是一个内置函数,因此不是变量的好名称。此外,将布尔值与真值进行比较有些毫无意义,因为if max==True:等同于if max:。@Jim Fasarakis Hilliard。谢谢。“我明白了。”约翰·科尔曼。如果我使用If max:结果是否会按预期无效?请详细说明。我是python新手,在网上学习。你到底是如何选择变量名max=guess.isnumeric的?奇怪的选择。把它叫做数字怎么样,最好还是不要把它赋给变量,直接测试ifguess.isnumeric我没说要检查字母参数。我已经提到,我想检查猜测参数是否是数字。这段代码是关于简单的字母匹配的,这就是为什么我想避免使用数字作为guess_check函数的参数。我刚刚给出了check_guess的例子,2如果你运行我发布的代码,它会显示低。@Rajan你在测试guess,但是H,2有无效的字母。。。也许可以改变你的论点?