简体   繁体   English

我如何从列表中随机选择用户要求在 python 中使用输入的次数?

[英]how do i randomly pick characters out of a list the amount of times the user asks for using input in python?

For my assignment i have to create a password generator using python.对于我的作业,我必须使用 python 创建一个密码生成器。 it has to ask the user how many characters long they want the password, then it has to create it by using random.randint and prints it out as a string.它必须询问用户他们想要密码的长度,然后它必须使用 random.randint 创建它并将其作为字符串打印出来。 how do i get the user input to multiply the random.randint bit the number of times they've asked for??我如何让用户输入将 random.randint 位乘以他们要求的次数?

this is what i have so far...........这就是我迄今为止所拥有的......

# imports modules
import random, time

# defines welcome function
def welcome():
    print('Welcome to the password generator!')
    time.sleep(2)
    print('This program will generate random passwords.')

# create a list
character_list = ['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','0','1','2','3','4','5','6','7','8','9']

# defines enter number function which asks the user to enter a number
def password_gen():
    i = 'Nothing yet'
    while i.isdigit() == False:

        i = input()
        if i.isdigit() == False:
            print('Please enter a number\n')

    return int(i)

for x in range(0,5):
    rc = character_list[random.randint(0,62)]
    print (rc)

# calls functions and runs the program
welcome()
print('Please press enter...')
password_gen()

This is very nearly there... 这几乎在那...

for x in range(0,5):
    rc = character_list[random.randint(0,62)]
    print (rc)

Change to a list-comp to build a single list of n items, eg: 更改为list-comp以构建n项目的单个列表,例如:

rc = [character_list[random.randint(0,62)] for _ in xrange(5)]

However, it's easier to use choice then generate an index into the list - then do as you're doing now but repeated... 但是,使用choice然后在列表中生成索引会更容易-然后像现在一样操作,但是重复...

from random import choice
from string import ascii_letters, digits

chars = ascii_letters + digits
# replace 10 with how many....
passwd = ''.join(choice(chars) for _ in xrange(10))
# create a list
character_list = ['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','0','1','2','3','4','5','6','7','8','9']

import random
pas = list() # random password

number = input() # 8
number = int(number) if number.isdigit() else 0

while(number):
    pas.append(random.choice(character_list))
    number -= nu

result = "".join(pas)
print(result)
# 'jjyMQPuK'

暂无
暂无

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

相关问题 如何计算使用python显示字母的次数? - How do I count the amount of times a letter appears using python? Python:如何获得一个程序来提示用户一定的固定时间来输入用户信息? - Python: How do you get a programme to prompt the user a certian fixed amount of times for a user input? 如何在 python 中的两个数字之间随机选择,但有例外? - How do I randomly pick between 2 numbers in python with exceptions? 当用户为变量分配了用户选择输入的“ x”次次数时,如何创建变量? - How do I create variables as they are being assigned by the user a 'x' amount of times that the user chooses to input? 你如何让 Python 向用户询问输入“x”的次数? - How do you get Python to ask the user for an input 'x' amount of times? 如何根据用户对第一个问题的输入提示用户 x 次 - How do I promt user x amount of times based on their input on the first question 如何进行验证循环以确保用户在 a.split 输入中输入正确的次数? - How do I make a validation loop to make sure the user inputs the correct amount of times in a .split input? 如何使用Python中的数组从用户输入字符列表 - How do you input a list of characters from the user using arrays in Python 当用户在 Python 中询问时,我如何回忆整个 function? - How do I recall an entire function when the user asks for it in Python? 你如何计算一个项目从随机列表中出现的次数? - How do you tally the amount of times a item in appears from a randomly list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM