1081 检查密码 python

1081 检查密码 (15 分)

本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。

输入格式:

输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。

输出格式:

对每个用户的密码,在一行中输出系统反馈信息,分以下5种:

  • 如果密码合法,输出Your password is wan mei.
  • 如果密码太短,不论合法与否,都输出Your password is tai duan le.
  • 如果密码长度合法,但存在不合法字符,则输出Your password is tai luan le.
  • 如果密码长度合法,但只有字母没有数字,则输出Your password needs shu zi.
  • 如果密码长度合法,但只有数字没有字母,则输出Your password needs zi mu.

输入样例:

5
123s
zheshi.wodepw
1234.5678
WanMei23333
pass*word.6

输出样例:

Your password is tai duan le.
Your password needs shu zi.
Your password needs zi mu.
Your password is wan mei.
Your password is tai luan le.

通过ord()来判断输入的是字母还是数字还是其他的,ord('.')=46,小写字母是97-122,大写字母是65-90,数字是48-57

建了个字典,遍历输入的密码,如果是字母,就在word上加1,如果是数字就在num上加1,如果是小数点,就在小数点上加1

这样在最后做判断的时候就很容易,比如:如果密码缺少字母,那么word的值就是0

需要注意的是,如果输入的密码中存在特殊字母(非字母非数字非小数点)的话,在输出相应句子以后,需要把字典里的所有key的值变为0,不然最后判断的时候会错

N = int(input())
for i in range(N):
    psw = input()
    dic={'word':0,'num':0,'.':0}
    if len(psw)<6:
        print('Your password is tai duan le.')
    if len(psw) >= 6:
        for j in psw:
            if (ord(j) not in range(97,123))and(ord(j) not in range(65,91))and(ord(j) not in range(48,58))and(ord(j)!=46):
               
                print('Your password is tai luan le.')
                dic={'word':0,'num':0,'.':0}
                break
            if (ord(j) in range(97,123)) or (ord(j) in range(65,91)):
                dic['word'] += 1
            if ord(j) in range(48,58):
                dic['num'] += 1
            if ord(j) == 46:
                dic['.'] += 1

        if dic['word'] != 0 and dic['num'] != 0:
            print('Your password is wan mei.')

        if dic['word'] == 0 and dic['num'] != 0:
            print('Your password needs zi mu.')
            
        if dic['word'] != 0 and dic['num'] == 0:
            print('Your password needs shu zi.')

猜你喜欢

转载自blog.csdn.net/weixin_43731183/article/details/87950246