简体   繁体   English

Python:随机密码生成器

[英]Python: Random Password Generator

I want o create a Random Password generating program.我想创建一个随机密码生成程序。 Passwords shown should have a minimum of 10 digits to a maximum of 20 digits.显示的密码至少应为 10 位,最多为 20 位。 However, the program didn't capture the length.但是,该程序没有捕获长度。

My code:我的代码:

import random
import string

def rpassword():
    while True:
        length = int(input('\nEnter the length of password: '))
        if length < 10:
            print("\nWarning: Password length should be more than 10!")
        elif length > 20:
            print ("\nWarning: Password length should be less than 20!")
        else:
            print("\nYour password is: ", password)
            break

lower = string.ascii_lowercase
upper = string.ascii_uppercase
number = string.digits
symbol = string.punctuation

pswd = lower + upper + number + symbol

passd = random.sample(pswd,length)
password = "".join(passd)   

rpassword()

#exampleoutput
#Hello, Welcome to Random Password Generator!

#Enter the length of password: 15

#Your password is:  +|VR{c<$k

this will work:这将起作用:

import string
import random

def rpassword():
    while True:
        length = int(input('\nEnter the length of password: '))
        if length < 10:
            print("\nWarning: Password length should be more than 10!")
        elif length > 20:
            print("\nWarning: Password length should be less than 20!")
        else:
            lower = string.ascii_lowercase
            upper = string.ascii_uppercase
            number = string.digits
            symbol = string.punctuation

            pswd = lower + upper + number + symbol

            passd = random.sample(pswd, length)
            password = "".join(passd)
            print("\nYour password is: ", password)
            break

rpassword()

output: output:

Enter the length of password: 15

Your password is:  cX7Mg<91G@0-.C"

You need to indent your code to have it happen inside the rpassword function (indent it so it's deeper than the def but not the while ), and you also need to print the password after you've generated it, not before:您需要缩进您的代码以使其在rpassword function 中发生(缩进它使其比def更深,但不是while ),并且您还需要在生成密码之后打印密码,而不是之前:

def rpassword():
    while True:
        length = int(input('\nEnter the length of password: '))
        if length < 10:
            print("\nWarning: Password length should be more than 10!")
        elif length > 20:
            print ("\nWarning: Password length should be less than 20!")
        else:
            break

    # At this point the loop has broken and 10 < length < 20.
    lower = string.ascii_lowercase
    upper = string.ascii_uppercase
    number = string.digits
    symbol = string.punctuation

    pswd = lower + upper + number + symbol

    passd = random.sample(pswd, length)
    password = "".join(passd)

    # Now we have a valid value for password!
    print("\nYour password is: ", password)

rpassword()

I might suggest having this function return the password and printing it outside the function -- that way if you want to do something with the password (like store it somewhere) it's easy to assign it to a value outside of the function instead of (or in addition to) printing it.我可能会建议让这个 function return密码并将其打印在 function 之外——这样,如果你想用密码一些事情(比如将它存储在某个地方),很容易将它分配给 ZC1C41145268E17A94D 之外的值(而不是除了)打印它。

def rpassword() -> str:
    while True:
        length = int(input('\nEnter the length of password: '))
        if length < 10:
            print("\nWarning: Password length should be more than 10!")
        elif length > 20:
            print("\nWarning: Password length should be less than 20!")
        else:
            break

    return "".join(random.sample(
        string.ascii_lowercase
        + string.ascii_uppercase
        + string.digits
        + string.punctuation,
        length
    ))


print("\nYour password is: ", rpassword())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM