【问题标题】:The simplest way to check for UPPERCASES检查大写的最简单方法
【发布时间】:2020-04-10 13:50:04
【问题描述】:

在这里,我尝试验证密码并检查密码中是否有大写和数字,然后将其保存为不同的变量。使用函数,我是否可以这样做。缩进在格式上已经改变,所以请在其他方面帮助我。


def length(long):
    while len(long) < 10:
        print("Please make your password longer, up to at least 10 characters.")
        print("Your password is only " + str(len(long) + " characters long")

if password.isupper() = True:
  print("Welcome to this student interface")
  username = input("Please enter a username")
  password = input("Please enter a strong password")
  length(password)

这就是我现在所做的:

def length(long):
    bool LengthCheck = False
    if len(long) < 10:
        print("Please make your password longer, up to at least 10 characters.")
        print("Your password is only " + str(len(long) + " characters long")
    else:
        LengthCheck = True

errors = []

print("Welcome to this student interface")
username = input("Please enter a username")
password = input("Please enter a strong password")
length(password)

bool Capcheck = False
bool DigCheck  = False

while CapCheck = False or CapCheck = False:
    length(password)

    if not any(x.isupper() for x in password):
        errors.append("Your password needs at least 1 capital.")
    else:
        CapCheck = True
        break

    if not any(x.islower() for x in password):
        errors.append("......... Why?")

    if not any(x.isdigit() for x in password):
        errors.append("You need to have at least 1 digit")
    else:
        DigCheck = True
        break

    if errors:
        print(" ".join(errors))
        password = input("Please enter a stronger password")

显然我的布尔值有错误,请帮助

def length(long):
    bool LengthCheck = False
    if len(long) < 10:
        print("Please make your password longer, up to at least 10 characters.")
        print("Your password is only " + str(len(long) + " characters long")
    else:
        LengthCheck = True

【问题讨论】:

    标签: python python-2.7 function validation


    【解决方案1】:

    尝试使用islower()

    password.islower()
    

    如果密码中没有大写字母,则返回 True


    现在如果你想检查它是否有第 i 个它,你必须关注@jubnvz:

    any(i.isdigit() for i in password)
    

    或更具体的方式:

    any(map(str.isdigit, password))
    

    对于您的密码条目,请尝试:

    while True:
        password = input(""Please enter a strong password:")
        if not any(x.isupper() for x in password):
            print("Your password needs at least 1 upper case.")
        elif not any(x.isdigit() for x in password):
            print("You need to have at least 1 digit")
        elif not any(x.islower() for x in password):
            print("Your password needs at least 1 lower case.")
        elif len(password) < 10:
            print("Please make your password longer, up to at least 10 characters.")
            print("Your password is only " + str(len(password)) + " characters long")
        else:
            break
    

    如果您也想确认密码,请尝试:

    while True:
        password = input(""Please enter a strong password:")
        if not any(x.isupper() for x in password):
            print("Your password needs at least 1 upper case.")
        elif not any(x.isdigit() for x in password):
            print("You need to have at least 1 digit")
        elif not any(x.islower() for x in password):
            print("Your password needs at least 1 lower case.")
        elif len(password) < 10:
            print("Please make your password longer, up to at least 10 characters.")
            print("Your password is only " + str(len(password)) + " characters long")
        else:
            passwordcon = input(""Please confirm your password:")
            if passwordcon == password:
                break
            else:
                print("Your passwords do not match, try again'")
    

    【讨论】:

      【解决方案2】:
       any([p.isupper() for p in password])    
      

      【讨论】:

      • 这样做的好处是可以给p添加更多验证,检查是否是符号,或者拒绝无效字符等。否则@InfinityTM的回答更简单
      • 我同意,@InfinityTM 的回答比较简单,请采纳。
      • 我已经注意到并调整了我的工作。欣赏它
      • 如果密码不合适,我可以要求对方重新输入密码吗?
      • 你可以。您需要修改代码以不断提示用户输入密码,并对其进行验证。如果有效,则继续,否则,再次提示。但是,您的问题范围已经改变。您可能想提出一个不同的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      相关资源
      最近更新 更多