Python检查输入中是否包含数字?

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)

Kas*_*mvd 5

您可以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)