Python编写一个程序,用户输入密码,密码长度必须大于等于 8位,如果密码满足 10 位以上,数字、大写字母、小写字母、标点符号中一种可以加1分,总计5分,1-5分依次对应密码安全等级为弱,较弱,中,较强和强,使用数字和星号输出密码等级。“
时间: 2023-09-23 19:12:07 浏览: 23
以下是代码实现:
```python
import re
password = input("请输入密码:")
# 检查密码长度是否大于等于8位
if len(password) < 8:
print("密码长度必须大于等于8位!")
else:
# 检查密码是否包含数字、大写字母、小写字母、标点符号中的一种或多种
score = 0
if re.search(r'\d', password):
score += 1
if re.search(r'[A-Z]', password):
score += 1
if re.search(r'[a-z]', password):
score += 1
if re.search(r'[^\w\s]', password):
score += 1
# 根据分数输出密码等级
if score == 1:
print("密码等级:弱(*)")
elif score == 2:
print("密码等级:较弱(**)")
elif score == 3:
print("密码等级:中(***)")
elif score == 4:
print("密码等级:较强(****)")
elif score == 5:
print("密码等级:强(*****)")
```
运行程序后,可以输入密码进行测试。例如,输入 `Abc123!#`,输出结果为:
```
密码等级:强(*****)
```