【问题标题】:how to make sure that a input has meets requirements [closed]如何确保输入满足要求[关闭]
【发布时间】:2013-09-26 21:25:11
【问题描述】:

用户必须输入密码,但密码中必须至少有1个大写、小写和数字。

password = input("Please enter a password: ")

【问题讨论】:

  • 尝试解决其中的一部分。那么剩下的就很容易了。如果您遇到困难,请向我们展示您的尝试。

标签: python if-statement input


【解决方案1】:
if any(x.isupper() for x in password) and \ 
   any(x.islower() for x in password) and \
   any(x.isdigit() for x in password):
    print ("Congratulations, you have a secure password.")

【讨论】:

    【解决方案2】:

    您还可以放入一个在匹配这些条件之前不会停止的 while 循环:

    while True:
         password = input("Please enter a password: ")
         if any(x.isupper() for x in password) and \ 
            any(x.islower() for x in password) and \
            any(x.isdigit() for x in password): #copy from Rob's awnser
                break
    
         else: print('Invalid!')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-24
      • 2014-11-15
      • 2013-10-20
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 2019-05-11
      • 2021-11-16
      相关资源
      最近更新 更多