0

I'm trying to make a password generator. I've made lists containing all of the numbers and letters. I'm trying to add all of the lists to one big list then pick a random thing out of a random list for each position.

def generator():
    password=['','','','','','','','','','','']
    symbols=['£','$','%','^','&','*','_']
    numbers=[0,1,2,3,4,5,6,7,8,9]
    letter=['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']
    Uletter=['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']
    password[0]=random.choice(numbers)
    password[1]=random.choice(numbers)
    password[2]=random.choice(numbers)
    password[3]=random.choice(numbers)
    password[4]=random.choice(numbers)
    password[5]=random.choice(numbers)
    password[6]=random.choice(numbers)
    password[7]=random.choice(numbers)
    password[8]=random.choice(numbers)
    password[9]=random.choice(numbers)
    password[10]=random.choice(numbers)
    print(password)

I know that I've got random.choice(numbers) for each slot but that is just for the sake of saving time right now until I get the generator working.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
  • I assume you are a new developer, so the best advice here is that this is not the way to generate a string. Please read the following answer to get more professional solution: https://stackoverflow.com/a/2257449/3767514 – Crispy Holiday Sep 29 '17 at 18:47
  • 1
    I'm new and I know about generating random strings and integers. The reason I want to add all the lists into one big list is so that each slot doesn't have to be either a number or integer. If i do that, the password is easy to predict as each slot is either a number, letter or a symbol. If I make each slot purely random, there is no real pattern as to whether a slot is number,letter or symbol. I know there are better ways of doing it, i'm just trying to get it done in a simple way. – Liam Michel Sep 29 '17 at 18:49
  • 1
    _"I want to add all the lists into one big list"_. That seems like a fine idea. Then you can use `choice` to get a list from that list-of-lists, and you can use `choice` a second time to get a character. Go ahead and try that approach. – Kevin Sep 29 '17 at 18:52
  • So you claim that a fixed-length password containing symbols is totally random? I disagree. But really it doesn't really matter - the code you are writing is very inefficient. I recommend reading about generating passwords in general. BTW, a possible way to do it is just encrypting the current timestamp using some encryption library. – Crispy Holiday Sep 29 '17 at 18:53
  • Why do you want a list of lists? Why not just put all the different chars into one big list? Or just put them into a string, since `random.choice` will happily choose from a string. BTW, if you _do_ want to make a list from a string the easy way is to do `list('some string')` – PM 2Ring Sep 29 '17 at 18:56
  • 1
    If you're trying to generate a password that has (for example) at least one capital letter, at least one number and at least one punctuation symbol, the approach of combining all the lists into one will not work. There would be no guarantee that `random.choice` would pick at least one of each kind of thing. – Paul Cornelius Sep 29 '17 at 19:04
  • @PaulCornelius yep. It would make sense to pick **one of each of those requirements**, then randomly pick from the rest of your set to fill the needed length, then finally, `shuffle` seems straightforward. – juanpa.arrivillaga Sep 29 '17 at 19:09
  • Of course, password rules that say you have to have at least one of each of these categories of character reduce the total number of possibilities, and hence make the password _weaker_. But I guess they make it harder for people to choose simple words that can be cracked using a dictionary-based attack. – PM 2Ring Sep 29 '17 at 19:11

2 Answers2

0
all_chars = symbols + numbers + letter + Uletter
password = ''.join(str(random.choice(all_chars)) for i in range(11))

Note that you have to include str() because the elements of numbers are integers, not strings.

Kerrick Staley
  • 1,583
  • 2
  • 20
  • 28
0

I'd normally go with @Kerricks answer, but to show you some other, slightly longer, options:

import random
import string

numbers = [str(x) for x in range(10)]
lower_case_letters = [x for x in string.lowercase]
upper_case_letters = [x for x in string.uppercase]
symbols = ['$', '%', '^', '&', '*', '_']

all_chars = numbers + lower_case_letters + upper_case_letters + symbols

password = ''

for x in range(11):
    password += random.choice(all_chars)

print(password)

This demonstrates how to generate a list of numbers, converted to strings. How to use the strings library to get uppercase and lowercase letters, how to concatenate lists, and how to use random.choice to pick a random choice from the full all_chars list. HTH.

Oh and by the way: do not, DO NOT, generate passwords like this in real life. Please.

Danielle M.
  • 3,607
  • 1
  • 14
  • 31
  • As I mentioned in the question comments, `random.choice` will happily accept a string as its arg, so you don't need to bother building those lists. But when you _do_ want to build a list from the chars of a string there's no need to use a loop, you can just pass the string to the `list` constructor, eg `list(string.ascii_letters)` – PM 2Ring Sep 30 '17 at 05:07