带有读取和随机模块的 Python 密码生成器

问题描述

我对 python 有点陌生,正在编写密码生成器。截至目前,我认为我处于需要解释的高原。 最后,我想用上面给出的用户输入生成密码,我收到一个类型错误 (类型错误:choice() 需要 2 个位置参数,但给出了 3 个) 我错过了什么,以至于 random.choice 功能不起作用


import random



Uppercaseletters = open("Upper.txt").read()
Lowercaseletters = open("Lower.txt").read()
Numbers = open("Zahlen.txt").read()
Symbols = open("Symbole.txt").read()


Upperbool = True
Lowerbool = True
Numbersbool = True
Symbolsbool = True

whole = ""

if Upperbool:
    whole += Uppercaseletters

if Lowerbool:
    whole += Lowercaseletters

if Numbersbool:
    whole += Numbers

if Symbolsbool:
    whole += Symbols

print("Hello and welcome to the simple password generator.")


a = 1
b = 1

if b <= 10:
    amount = int(input("How many passwords do you want to generate? "))
else:
    print("You are exceeding the limit of a maximum of 10 Passwords")
    
# length auswählen lassen (maximal 20 Zeichen lang (Fehler prevention))
if a <= 20:     
    length = int(input("How long do you want your password to be? "))
else:
    print("That password will be too long,try a number below 20")

for x in range(amount):
    password = "".join(random.choice(whole,length))
    print(password)

解决方法

我相信您正在寻找这样的东西:

import random


Uppercaseletters = open("Upper.txt").read()
Lowercaseletters = open("Lower.txt").read()
Numbers = open("Zahlen.txt").read()
Symbols = open("Symbole.txt").read()

Upperbool = True
Lowerbool = True
Numbersbool = True
Symbolsbool = True

whole = ""

if Upperbool:
    whole += Uppercaseletters

if Lowerbool:
    whole += Lowercaseletters

if Numbersbool:
    whole += Numbers

if Symbolsbool:
    whole += Symbols

print("Hello and welcome to the BMD's simple password generator.")


amount = 100
length = 100

while amount>10:
    amount = int(input("How many passwords do you want to generate? "))
    if amount>10:
        print("You are exceeding the limit of a maximum of 10 Passwords")

while length>20:
    length = int(input("How long do you want your password to be? "))
    if length>20:
        print("That password will be too long,try a number below 20")


for passwords in range(amount):
    password = ""
    for character in range(length):
        password = password + random.choice(list(whole))
    print(password)

我修改了它,使其不允许超过 10 的数量和超过 20 的长度。