Python-检查字符串是否包含数字 [英] Python - Check if a string contains a digit

查看:86
本文介绍了Python-检查字符串是否包含数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a function that uses a while True loop to ask the user to input a password that passes the criteria; min8-15max characters in length and includes at least one integer. I am stumped on how to properly check the input for an integer.

我的程序:

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

推荐答案

尝试:

if not any(c.isdigit() for c in pw)

代替

if int not in pw:
    print('Password must contain at least one integer.')

int 是类型对象,您要检查字符0-9的存在.

int is a type object and you want check the presence of characters 0-9.

这篇关于Python-检查字符串是否包含数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆