Python密码生成器

时间:2017-05-18 17:20:18

标签: python python-2.7 passwords

我正在尝试在Python中创建一个必须包含大写字母,小写字母和数字的密码生成器,并且长度必须介于6到20个字符之间。

   import random
   import string
   def password_gen():
        while True:
            length = random.randint(6,20)
            pwd = []
            for i in range(length):
                prob = random.random()
                if prob < 0.34:
                    char = random.choice(string.ascii_lowercase)
                    pwd.append(char)
                elif prob < 0.67:
                    char = random.choice(string.ascii_uppercase)
                    pwd.append(char)
                else:
                    char = str(random.randint(0,9))
                    pwd.append(char)
            pwd = ''.join(pwd)
            #check password here
            return pwd

但是,我无法检查密码以确保其中包含前面列出的所需字符。我不确定是否/如何使用继续声明。非常感谢任何帮助!

5 个答案:

答案 0 :(得分:1)

我认为如果您只是提前处理这些要求,这将更容易确保您满足基本要求。

import random
import string

def generate_pw():
    length = random.randint(6,20) - 3
    pwd = []
    pwd.append(random.choice(string.ascii_lowercase))
    pwd.append(random.choice(string.ascii_uppercase))
    pwd.append(str(random.randint(0,9)))
    # fill out the rest of the characters
    # using whatever algorithm you want
    # for the next "length" characters
    random.shuffle(pwd)
    return ''.join(pwd)

这将确保您的密码包含您需要的字符。对于其他字符,您可以使用所有字母数字字符列表并调用random.choice length次。

答案 1 :(得分:1)

您可以使用isupper()和islower()函数来获取密码是否包含大写和小写。

e.g。

upper=0
lower=0
for i in range(length):
    if (pwd[i].islower()):
        upper=1
    elif (pwd[i].isupper()):
        lower=1

答案 2 :(得分:0)

import random
import string


def password_gen():
    lower_case_letter = random.choice(string.ascii_lowercase)
    upper_case_letter = random.choice(string.ascii_uppercase)
    number = random.choice(string.digits)
    other_characters = [
        random.choice(string.ascii_letters + string.digits)
        for index in range(random.randint(3, 17))
    ]

    all_together = [lower_case_letter, upper_case_letter] + other_characters

    random.shuffle(all_together)

    return ''.join(all_together)

答案 3 :(得分:0)

<块引用>

密码生成器更细分,你可以得到任何你想要的数字,但它输出一个模式,先添加字母,然后是数字,最后是符号

import random 
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']

print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))



passwordlength = nr_letters + nr_numbers + nr_symbols


chars = ""
for x in range (0, nr_letters): 
  char = random.choice(letters) 
  chars += char

nums = ""
for y in range (0, nr_numbers):
  num = random.choice(numbers)
  nums+=num

syms = "" # string accumulator 
for z in range (0, nr_symbols): 
  symb = random.choice(symbols)
  syms += symb

print(f"Here is your password: {chars}{nums}{syms}")

答案 4 :(得分:-2)

import random
import time


Uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Lowercase = "abcdefghijklmnopqrstuvwxyz"
Digits = "0123456789"
Symbols = "()!?*+=_-"

Mixedbag = Uppercase + Lowercase + Digits + Symbols

#This function allwos the user to generate a random password containing x 
amount of uppercase, lowercase and symbols to form a password

def generate():
    print ("Starting the word generating process")

    time.sleep(1)

    while True:
        word_length = random.randint(8, 20)

        Madeword = ""
        for x in range(word_length):
            ch = random.choice(Mixedbag)
            Madeword = Madeword + ch
        break
    print ("Your word is ", Madeword)


import re