【问题标题】:how would i create a program in python to implement this?我将如何在 python 中创建一个程序来实现这个?
【发布时间】:2021-05-07 07:48:16
【问题描述】:

密码必须至少包含八个字符。 • 密码仅由字母和数字组成。 • 密码必须至少包含两位数字

【问题讨论】:

标签: python-3.x


【解决方案1】:

您可以参考此代码作为您的答案:

import re

def validate():
  while True:
    password = input("Enter a password: ")
    if len(password) < 8:
        print("Make sure your password is at lest 8 letters")
    elif re.search('[0-9]',password) is None:
        print("Make sure your password has a number in it")
    elif re.search('[A-Z]',password) is None: 
        print("Make sure your password has a capital letter in it")
    else:
        print("Your password seems fine")
        break

   

validate()

【讨论】:

    【解决方案2】:

    如果您想生成此类密码,请使用以下代码:

    import string
    import random
    
    letters =string.ascii_letters
    digits = string.digits
    comb = letters + digits
    
    length = random.randint(6,98)
    
    password = random.choices(digits,k = 2)
    password+= random.choices(comb, k =length)
    random.shuffle(password)
    password = ''.join(password)
    
    print(password)
    

    我假设密码的最大长度为 100。您可能需要更改它。

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2022-12-03
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 2018-06-16
      • 1970-01-01
      • 2015-04-26
      • 1970-01-01
      相关资源
      最近更新 更多