我正在尝试制作密码强度测试仪。密码需要至少包含4个数字和6个字母,因此我需要找出用户在raw_input
中输入的数量。
答案 0 :(得分:9)
text = "helloworld42"
numbers = sum(c.isdigit() for c in text)
letters = sum(c.isalpha() for c in text)
答案 1 :(得分:3)
只需迭代密码:
import string
numbers = 0
letters = 0
for letter in raw_input('Enter a password: '):
if letter in string.ascii_letters:
letters += 1
elif letter in string.digits:
numbers += 1
print numbers, letters