Python检查输入中是否有数字?
我想看看我如何能看到,如果一个数字是在用户输入。我尝试使用Python检查输入中是否有数字?,python,string,input,numbers,python-3.4,Python,String,Input,Numbers,Python 3.4,我想看看我如何能看到,如果一个数字是在用户输入。我尝试使用.isdigit(),但只有当它只是一个数字时才有效。我正在尝试将其添加到密码检查器。我还尝试了.isalpha(),但没有成功。我做错了什么?我需要添加或更改什么 这是我的 password = input('Please type a password ') str = password if str.isdigit() == True: print('password has a number and l
.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!')`
您可以尝试
re.search
if re.search(r'\d', password):
print("Digit Found")
不要将内置数据类型用作可变名称。您可以在函数中使用生成器表达式和
isdigit()
:
if any(i.isdigit() for i in password) :
#do stuff
def any(iterable):
for element in iterable:
if element:
return True
return False
使用any
的优点是它不会遍历整个字符串,如果它第一次找到一个数字,就会返回bool值
它等于衰减函数:
if any(i.isdigit() for i in password) :
#do stuff
def any(iterable):
for element in iterable:
if element:
return True
return False
@ Vias-RaJ回答了你的问题,但是你真的应该考虑(或者使用Python版本)来处理你的用例。