Python 检查字符串是否只包含一些值,而不包含其他值 print(“您好,欢迎使用密码强度。测试您的密码todai有多强!”) password=input(“我们输入一个密码,你为什么不……”) 打印(“因此您的密码是”,password) 打印(“好吧,让我们看看我能从中理解什么…”) 如果len(密码)>设置('1234')>>设置('a1234')

Python 检查字符串是否只包含一些值,而不包含其他值 print(“您好,欢迎使用密码强度。测试您的密码todai有多强!”) password=input(“我们输入一个密码,你为什么不……”) 打印(“因此您的密码是”,password) 打印(“好吧,让我们看看我能从中理解什么…”) 如果len(密码)>设置('1234')>>设置('a1234'),python,Python,这就是我想问的密码是否只包含数字,但我不知道如何做,我已经尝试使用ands和ors,但失败惨重。您可以做: print("Hello, welcome to password strength. Test how strong your password is todai!") password = input("Well enter a password why don't you... ") print("So your password is", password) print("Well

这就是我想问的密码是否只包含数字,但我不知道如何做,我已经尝试使用ands和ors,但失败惨重。

您可以做:

print("Hello, welcome to password strength. Test how strong your password is todai!")
password = input("Well enter a password why don't you... ")
print("So your password is", password)
print("Well ok, let's see what i can understand from this...")

if len(password) < 6:   
    print("Your password is too short")    
else:    
    print("Your password is of a good length")    

if password == password.upper():    
    print("Your password has too many uppercase letters")
else:    
    print("Your password has 0 or a couple upper case letters, please consider making your password remember-able.")

if password == password.lower():    
    print("Your password needs a upper case letter case letters")
else:    
    print("Your password has a suitable amount of lowercase vs upper case letters")

if password == 
集合有几个有用的功能,例如检查子集:

>>> set('swordfish')
{'d', 'f', 'h', 'i', 'o', 'r', 's', 'w'}

>>> set('aaaaassssdddfff')
{'a', 'd', 'f', 's'}

>>> set('1234')
{'1', '2', '3', '4'}
>>设置('1234')>>设置('a1234')您可以执行以下操作:

print("Hello, welcome to password strength. Test how strong your password is todai!")
password = input("Well enter a password why don't you... ")
print("So your password is", password)
print("Well ok, let's see what i can understand from this...")

if len(password) < 6:   
    print("Your password is too short")    
else:    
    print("Your password is of a good length")    

if password == password.upper():    
    print("Your password has too many uppercase letters")
else:    
    print("Your password has 0 or a couple upper case letters, please consider making your password remember-able.")

if password == password.lower():    
    print("Your password needs a upper case letter case letters")
else:    
    print("Your password has a suitable amount of lowercase vs upper case letters")

if password == 
集合有几个有用的功能,例如检查子集:

>>> set('swordfish')
{'d', 'f', 'h', 'i', 'o', 'r', 's', 'w'}

>>> set('aaaaassssdddfff')
{'a', 'd', 'f', 's'}

>>> set('1234')
{'1', '2', '3', '4'}
>>set('1234')>>set('a1234')使用
isdigit()

使用
isdigit()


字符串只有在可以转换为int时才有数字,所以

>>> "abcd123".isdigit()
False

>>> "123".isdigit()
True

字符串只有在可以转换为int时才有数字,所以

>>> "abcd123".isdigit()
False

>>> "123".isdigit()
True

对这类问题使用regexp。对这类问题使用regexp。这是如何工作的+1它确实在代码中工作,只是想知道如何工作?@Razor我添加了一个解释。令人惊讶的、简短而简单的解决方案。也很快!这是怎么回事+1它确实在代码中工作,只是想知道如何工作?@Razor我添加了一个解释。令人惊讶的、简短而简单的解决方案。也很快!
>>> "abcd123".isdigit()
False

>>> "123".isdigit()
True
try:
    int(password)
except ValueError:
    print("not parseable to int, so not only numbers")
else:
    print("Only numbers")