输入一串字符作为密码,密码可由数字或字母组成。编程Python程序判断输入的密码的强度,并输出。判断标准如下,满足其中一条,密码强度增加一级:(1)有数字,(2)有大写或小写字母(3)位数不少于8位
时间: 2024-02-27 10:56:49 浏览: 26
好的,下面是Python代码实现:
```python
def judge(password):
level = 0
# 判断是否有数字
if any(char.isdigit() for char in password):
level += 1
# 判断是否有大写字母
if any(char.isupper() for char in password):
level += 1
# 判断是否有小写字母
if any(char.islower() for char in password):
level += 1
# 判断密码长度是否大于等于8
if len(password) >= 8:
level += 1
# 输出密码强度
if level == 0:
print("密码强度:弱")
elif level == 1 or level == 2:
print("密码强度:中")
elif level >= 3:
print("密码强度:强")
```
函数中通过遍历密码中的每一个字符来判断其是否为数字或字母,并记录密码强度。最后根据密码强度输出对应的密码强度等级。
相关问题
python输入一串字符作为密码,密码只能由数字与字母组成
好的,这是一个常见的密码输入问题,您可以使用Python中的input函数来获取用户输入的密码,然后使用isalnum()方法来检查密码是否只由数字和字母组成。以下是一个示例代码:
```
password = input("请输入您的密码:")
if password.isalnum():
print("您的密码符合要求!")
else:
print("您的密码不符合要求,请重新输入!")
```
在上面的代码中,input函数用于获取用户输入的密码,isalnum()方法用于检查密码是否只由数字和字母组成。如果密码符合要求,则输出“您的密码符合要求!”;否则输出“您的密码不符合要求,请重新输入!”。
用python编写程序 输入密码字符串,判断密码强度
你好,我是C知道。以下是我的回答:
可以使用以下方法来判断密码强度:
1. 密码长度:密码长度应该至少为8个字符。
2. 大小写字母、数字和符号:密码应该包含大小写字母、数字和符号。
3. 避免常见密码:密码不应该是常见的密码,如"123456"或"password"。
4. 避免个人信息:密码不应该包含个人信息,如生日或姓名。
以下是一个用Python编写的程序,可以判断密码强度:
```python
def check_password_strength(password):
# 判断密码长度
if len(password) < 8:
return "密码强度较弱,密码长度应至少为8个字符。"
# 判断是否包含大小写字母、数字和符号
has_uppercase = False
has_lowercase = False
has_digit = False
has_symbol = False
for char in password:
if char.isupper():
has_uppercase = True
elif char.islower():
has_lowercase = True
elif char.isdigit():
has_digit = True
else:
has_symbol = True
if not (has_uppercase and has_lowercase and has_digit and has_symbol):
return "密码强度较弱,密码应包含大小写字母、数字和符号。"
# 判断是否为常见密码
common_passwords = ["123456", "password", "123456789", "12345678", "12345", "1234567", "1234567890", "qwerty", "abc123", "111111", "123123", "admin", "letmein", "welcome", "monkey", "login", "passw0rd", "starwars", "1234", "dragon", "football", "1234567898", "654321", "1234567899", "123321", "666666", "shadow", "master", "66666666", "qwertyuiop", "123456789a", "123456789b", "123456789c", "123456789d", "123456789e", "123456789f", "123456789g", "123456789h", "123456789i", "123456789j"]
if password in common_passwords:
return "密码强度较弱,密码为常见密码。"
# 判断是否包含个人信息
if "birth" in password.lower() or "name" in password.lower():
return "密码强度较弱,密码包含个人信息。"
# 密码强度较强
return "密码强度较强。"
password = input("请输入密码:")
print(check_password_strength(password))
```
希望这个程序能够帮助你判断密码强度。