Python - 检查字符串是否包含数字

k33*_*k33 3 python input

我正在制作一个函数,它使用 while True 循环来要求用户输入通过条件的密码;长度最少为 8-15 个字符,并且至少包含一个整数。我对如何正确检查整数的输入感到困惑。

我的程序:

def enterNewPassword():
    while True:
        pw = input('Please enter a password :')
        for i in pw:
            if type(i) == int:
                if len(pw) >= 8 and len(pw) <= 15:
                    break
        if int not in pw:
            print('Password must contain at least one integer.')
        if len(pw) < 8 or len(pw) > 15:
            print('Password must be 8 and no more than 15 characters in length.')
    return pw
Run Code Online (Sandbox Code Playgroud)

Mik*_*maa 6

尝试:

if not any(c.isdigit() for c in pw)
Run Code Online (Sandbox Code Playgroud)

代替

if int not in pw:
    print('Password must contain at least one integer.')
Run Code Online (Sandbox Code Playgroud)

int是一个类型对象,您想要检查字符 0-9 是否存在。