题目内容: 校验密码是否合法:编写程序提示用户输入一个密码,要求长度在5-10位,密码里必须包含大写字母、小写字母和数字。根据用户输入会返回相应提示信息:如果密码长度不合法,返回“The length of password must in range of 5-10”;如果密码内容不合法,返回"Password must include uppercase,lowercase and digit",否则返回“Password input success”。提示:可利用S.isdisjoint(T)方法,如果集合S与T没有相同元素,返回True。 输入格式: 一个字符串 输出格式: 一个字符串 输入样例: asdQEWr123 输出样例: Password input success 时间限制:500ms内存限制:32000kb
时间: 2024-03-07 20:47:46 浏览: 34
以下是Python代码实现:
```python
password = input()
if len(password) < 5 or len(password) > 10:
print("The length of password must in range of 5-10")
else:
if set(password).isdisjoint(set('ABCDEFGHIJKLMNOPQRSTUVWXYZ')):
print("Password must include uppercase")
elif set(password).isdisjoint(set('abcdefghijklmnopqrstuvwxyz')):
print("Password must include lowercase")
elif set(password).isdisjoint(set('0123456789')):
print("Password must include digit")
else:
print("Password input success")
```
解释如下:
首先,我们通过 input() 函数获取用户输入的密码,并将其存储在变量 password 中。
接下来,我们使用 len() 函数获取密码的长度,并检查其是否符合要求。如果长度不在 5 到 10 之间,则输出 "The length of password must in range of 5-10"。
如果密码长度符合要求,我们将使用 isdisjoint() 方法来检查密码中是否包含大写字母、小写字母和数字。如果其中任意一个字符集合与密码中的字符集合有相同的元素,则说明密码中包含了该类型的字符。否则,我们将输出相应的提示信息。
如果密码符合所有要求,则输出 "Password input success"。