开发者社区> 问答> 正文

Python-为什么即使我输入了一个应该有效的密码,它仍然说密码被拒绝?

已解决

image.png

我正在尝试制作一个密码程序,该程序必须包含一个大写字母、一个小写字母和一个数字。 但是 if 语句似乎无法正常工作,每当我输入“Py11”之类的密码时,它都会显示密码被拒绝。

展开
收起
1780169608831412 2021-10-18 20:11:50 687 0
1 条回答
写回答
取消 提交回答
  • 网络规划设计师、敏捷专家、CISP、ITSS服务经理、ACA全科目、ACP4项、ACE、CBP、CDSP、CZTP等。拥有 PRINCE2 Foundation/Practitioner、CCSK、ITIL、ISO27001、PMP等多项国际认证。 专利5+、期刊10+、知识产权师。核心期刊审稿人。
    采纳回答

    您好, 对于第一个循环,如果它是上层(不是),则您将在字母“h”中,否则您将运行密码拒绝部分(这就是问题)

    为了完成你想要的,我建议像这样检查:

    if condition1:
        pass #add here the counting you are making
    elif condition2:
        pass #add here the counting you are making
    elif condition3:
        pass #add here the counting you are making
    # [...]
    else:
        print("password denied")
    
    

    并且对于您检查上限、下限、数字的条件,如果您想允许特殊字符,您还需要检查它们

    其他方式你可以做到:

    userpass= input('Enter a password with at least one uppercase letter, one lowercase letter, and one number: ')
    uppercounter=0
    lowercounter=0
    numbercounter=0
    for i in range(len(userpass)):
        if userpass[i].isupper():
            uppercounter=uppercounter+1
        if userpass[i].islower():
            lowercounter=lowercounter+1
        if userpass[i].isnumeric():
            numbercounter=numbercounter+1
    if uppercounter == 0 or lowercounter == 0 or numbercounter == 0:
        print("Password denied")
        raise SystemExit(0)
    
    

    这样你就不必检查其他特殊字符

    2021-10-18 20:12:45
    赞同 2 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载