the*_*100 3 python string numbers input python-3.4
我正在尝试查看如何查看用户输入的数字。我尝试使用,.isdigit()
但仅在数量上有效。即时通讯试图将其添加到密码检查器。我也尝试过,.isalpha()
但是没有用。我做错了什么,我需要添加或更改什么?
这是我所拥有的
password = input('Please type a password ')
str = password
if str.isdigit() == True:
print('password has a number and letters!')
else:
print('You must include a number!')`
Run Code Online (Sandbox Code Playgroud)
您可以isdigit()
在any
函数中使用生成器表达式和:
if any(i.isdigit() for i in password) :
#do stuff
Run Code Online (Sandbox Code Playgroud)
使用的优点any
是它不会遍历整个字符串,并且如果它第一次找到一个数字,它将返回布尔值!
它等于休闲功能:
def any(iterable):
for element in iterable:
if element:
return True
return False
Run Code Online (Sandbox Code Playgroud)