Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.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 .isalpha打印为False,但选中时为True_Python_Passwords_Alphanumeric_Isalpha - Fatal编程技术网

Python .isalpha打印为False,但选中时为True

Python .isalpha打印为False,但选中时为True,python,passwords,alphanumeric,isalpha,Python,Passwords,Alphanumeric,Isalpha,这是用于检查密码是否为9个字符长、字母数字且至少包含1个数字的函数的一部分。理想情况下,我应该能够使用第一个if语句,但奇怪的是,它没有运行。我不明白为什么test1.isalpha在if语句中运行为“True”,但打印为“False” test1 = 'abcd12345' if len(test1) == 9 and test1.isalnum and not(test1.isalpha) print('This should work.') if len(test1) ==

这是用于检查密码是否为9个字符长、字母数字且至少包含1个数字的函数的一部分。理想情况下,我应该能够使用第一个if语句,但奇怪的是,它没有运行。我不明白为什么test1.isalpha在if语句中运行为“True”,但打印为“False”

test1 = 'abcd12345'

if len(test1) == 9 and test1.isalnum and not(test1.isalpha)
    print('This should work.')



if len(test1) == 9 and test1.isalnum:
    if (test1.isalpha):
        print('test1 is', test1.isalpha())

>>>('test1 is', False)        

如果test1.isalpha()而不是test1.isalpha

test1.isalpha
是一种方法,
test1.isalpha()
将返回结果
True
False
。当您检查条件方法是否始终满足时。另一个取决于结果

看看你的羞怯

In [13]: if test1.isalpha:
    print 'test'
else:
    print 'in else'
   ....:     
test

In [14]: if test1.isalpha():
    print 'test'
else:
    print 'in else'
   ....:     
in else
在if(
if(test1.isalpha):
)中,您正在测试方法实例,而不是该方法的结果


您必须使用
if(test1.isalpha()):
(括号)

类似的东西怎么样

  • len(test1)=9以确保长度为9
  • hasNumbers(inputString)
    函数返回字符串中任意数字的
    char.isdigit()
  • re.match(“^[A-Za-z0-9]*$”,test1)
    确保使用
重新导入
test1='abcd12345'
def hasNumber(输入字符串):
返回any(inputString中char的char.isdigit())
如果重新匹配(“^[A-Za-z0-9]*$”,test1)和hasNumber(test1)和len(test1)==9:
print('Huzzah!')

在一些方法调用之后,您缺少了
()