如何使程序要求用户输入长度并使用输入来制作用户想要的密码

发布于 2025-01-17 08:45:03 字数 759 浏览 3 评论 0原文

#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")

这不会让我出于某种原因发布评论

这是几天前我启动Python的代码IV,所以我很陌生

fist fist,我尝试放置一个可变而且,询问用户输入并将输入插入程序并使用它来查找应为此获得帮助的时间的长度吗?

#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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情癖 2025-01-24 08:45:03

我重新编写了您的代码。
您可以阅读评论以查看正在发生的事情。
本质上,我们有一个字符列表,这些字符将在密码中使用。
然后,我们向用户询问其密码的长度,然后将其转换为一个数字。
之后,我们循环浏览长度,并在密码中添加一个随机字符。

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)

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)
世界和平 2025-01-24 08:45:03

代码 - 您的代码需要进行细微更改

# 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")

您从用户收到的输入是 str 类型,因此您需要将其转换为 int 数据类型。

输出


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

建议:

  • 坚持使用 _ 或驼峰命名法。 pass_length1randomCharacter 有两种样式。
  • 尽量让你的函数名称易于理解。使用generate_random_passwordgeneratrandompaassword
  • =前后留出一些空间。

阅读一些有关 Python PEP 标准的内容,以使您的代码更具可读性。

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.

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. pass_length1 and randomCharacter are of two styles.
  • try to keep your functions names easy to understand. use generate_random_password than generatrandompaassword
  • Give some space to = before and after.

Read a bit about python PEP standards to make your code more readable.

快乐很简单 2025-01-24 08:45:03

在此处输入图像描述

这是我解决此问题的方法,我无法从上面获得正确的输出代码所以测试我的方式,祝你好运

enter image description here

It is my way to solve this , I could not get right output from above codes so test my way, good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文