Python .isalpha打印为False,但选中时为True
这是用于检查密码是否为9个字符长、字母数字且至少包含1个数字的函数的一部分。理想情况下,我应该能够使用第一个if语句,但奇怪的是,它没有运行。我不明白为什么test1.isalpha在if语句中运行为“True”,但打印为“False”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) ==
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!')
在一些方法调用之后,您缺少了()
。