简体   繁体   English

如何让程序要求用户输入长度并使用输入来生成密码,只要用户愿意

[英]how to make the program ask the user for a input for length and use the input to make the password as long as the user would like

#random password generator
import random
unified_code = "awertyuiosqpdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()_+"

passlength1=input("how long should your password be")
pass_length2=input("how long should your password be")

def generatrandompaassword():
    length = random.randint(passlength1,pass_length2 )

    password = "" 

    for index in range(length):
        randomCharacter = random.choice(unified_code)
        password = password + randomCharacter



    return password

passworder = generatrandompaassword()
print(passworder)
print("This is your new password")

this won't let me post for some reason what is a comment这不会让我出于某种原因发表什么是评论

this is the code iv made I started python a couple of days ago so I'm pretty new to it这是我几天前开始使用 python 编写的代码,所以我对它很陌生

fist I tried putting one-variable and than asking the user for the input and inserting the input into the program and using that to find the length of how long the password should be can get help with this?首先,我尝试放置一个变量,而不是要求用户输入并将输入插入程序并使用它来查找密码应该多长的长度可以获得帮助吗?

I re-wrote your code.我重写了你的代码。 You can read the comments to see what is happening.您可以阅读评论以了解发生了什么。 Essentially, we have a list of characters that will be used in the password.本质上,我们有一个将在密码中使用的字符列表。 We then ask the user for the length of their password, and convert it to a number.然后我们询问用户密码的长度,并将其转换为数字。 After, we loop through the length and add a random character to the password.之后,我们遍历长度并向密码添加一个随机字符。

import random
characters = "awertyuiosqpdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()_+"

# Get the length of the password, and cast it to an integer so it can be used in the for loop ahead
length = int(input("how long should your password be? "))

def generatrandompaassword():
    password = ""

    # For every character in the password, get a random character and add that to the password
    for i in range(length):
        password += random.choice(characters)

    return password

# Get the password
password = generatrandompaassword()
print("This is your new password: " + password)

Code - Your code needed minor changes代码 - 您的代码需要稍作改动

# random password generator
import random
unified_code = "awertyuiosqpdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890!@#$%^&*()_+"

pass_length1 = int(input("What should be the min length your password be: "))
pass_length2 = int(input("What should be the max length your password be: "))

def generatrandompaassword():
    length = random.randint(pass_length1, pass_length2 )
    password = "" 
    for index in range(length):
        randomCharacter = random.choice(unified_code)
        password = password + randomCharacter
    return password

passworder = generatrandompaassword()
print(passworder)
print("This is your new password")

The input your receive from the user is of type str , so you need to convert it into int data type.您从用户那里收到的输入是str类型的,因此您需要将其转换为int数据类型。

Output Output


What should be the min length your password be: 5
What should be the max length your password be: 15
vyA7ROviA
This is your new password

Suggestions:建议:

  • stick to either using _ or camelCasing.坚持使用_或 camelCasing。 pass_length1 and randomCharacter are of two styles. pass_length1randomCharacter是两个 styles。
  • try to keep your functions names easy to understand.尽量使您的函数名称易于理解。 use generate_random_password than generatrandompaassword使用generate_random_password不是generatrandompaassword
  • Give some space to = before and after.=前后给一些空间。

Read a bit about python PEP standards to make your code more readable.阅读一些关于 python PEP 标准的内容,使您的代码更具可读性。

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

相关问题 在绘制线性方程时,如何让我的程序询问用户输入 - How to make my program ask for user input when graphing linear equations 如何使用用户输入使程序结束? - How to make a program end using user input? 如何要求用户输入使代码交互 - How to ask user's input to make a code interactive 如何使用 Python 类并要求用户输入? - How to use a Python class and ask for user input? 如何让一个 python 文件要求用户输入,然后将此输入传递给第二个 .py 文件? - How can I make one python file ask for user input and then pass this input to a second .py file? 如何在 Python 3 中进行用户输入 - How to make user input in Python 3 是否可以将用户输入隐藏为“sudo”密码输入? - Is it possible to make user input invisible as a 'sudo' password input? 我如何让我的程序向用户提出更多问题? - How do i make my program ask further questions to the user? 如何让 python 在 def main() 中只要求用户输入一次? - How do I make python only ask for user input once in def main()? 我将如何制作它,以便我可以在该程序开始时询问用户是否想要执行它,因为下面的代码会带来语法错误 - How would I make it so I can ask the user at the start of this program in the want to execute it because the below code brings back syntax errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM