-2

I know this question has been asked a lot but I can't seem to get it to work from the previous answers given.

This is what I've got so far.

import string
import random
print("---------Password Generator---------")

letterslc = string.ascii_letters
lettersupc = letterslc.upper()
numbers = string.digits
punctuation = string.punctuation

lengthofpass = input("How many characters would you like your password to be?")
lengthofpass = int(lengthofpass)

I want the password to consist of numbers, letters and symbols the password length to be taken from user input.

I don't have any formal training just reading here and there online. I dont' know where to go from here.

Any help would be nice.

Tenkin
  • 25
  • 5
  • 1
    Write a while loop that runs lengthofpass no of times and pick random character from each of those lists and concat them into a string! This would be easy if you look into some documentation of python random - https://docs.python.org/3/library/random.html – Agent_Orange Jun 14 '20 at 06:12

3 Answers3

1

To generate random words from the given letters, numbers and punctuation, you can use random.choice Documentation
The code will be as follows:

password = ''.join(random.choice(letterslc + lettersupc + numbers + punctuation) for _ in range(lengthofpass))

Sample of passwords when lengthofpass = 10

9C-BOZT+)G
&ZXWn*>CDD
Ox'BJXxBHj
Mohnish
  • 1,010
  • 1
  • 12
  • 20
  • thank you. I saw this solution but did not understand the last part "for_in range" what does the _ mean? – Tenkin Jun 14 '20 at 06:23
  • @Tenkin you can replace it using `i` for example: `for i in range(lengthofpass)` as there is no real need for `i` it is replaced by `_` i.e. throwaway variable. More detailed explanation [here](https://stackoverflow.com/questions/5893163/what-is-the-purpose-of-the-single-underscore-variable-in-python) – Mohnish Jun 14 '20 at 06:27
  • 1
    @Tenkin it's mapping expression he used, you can look into https://howto.lintel.in/writing-shorthand-statements-in-python/ to learn the kind of statement! – Agent_Orange Jun 14 '20 at 06:30
0

Welcome to Stack Overflow! Contrary to how it may seem, we don't necessarily write code to fix your bugs, and all the contributors on this amazing website are here to help assist you in answering a question.

Anyways, with your problem- there are a couple improvements you could do to the code before generating the actual password, such as turning the strings into one big array of characters which are valid to use in the password. You can accomplish that like so:

import string
# You don't need to use the .upper(), as ascii_letters already contains the uppercase letters
# You can use the list() function to turn a string into an array, splitting it per character
# You can use the + operator to merge strings together
validChars = list(string.ascii_letters + string.digits + string.punctuation)

Also, you don't need to use a new line in order to use the int() function, and you can wrap it around the input() command like so:

passLength = int(input("Pass length? "))

Finally, for actually generating the password. For that, you can use a for loop to iterate over each letter, and select a character from the array.

passW = ""
for letter in range(passLength):
    # random.choice() picks a random item from an array
    # In this case, it's a random character from our list of valid characters
    passW += random.choice(validChars)

The last thing you need to do is print the password to the screen, so the user can use it:

print(passW)

I haven't tested any of the above code, but that's the gist of it. Please don't actually use this code to generate a random password, the random module isn't safe for that. Instead, use a module which generates cryptographically safe passwords, like the secrets module.

Xiddoc
  • 3,369
  • 3
  • 11
  • 37
0

There are many ways you could do this.

One way to do this would be to use the random.sample method (Although notice that the passwords can't have more than 94 characters, since the sample size can't be bigger than the population size).

import string
import random
print("---------Password Generator---------")

letterslc = string.ascii_letters
#There is no need of the command below since string.ascii_letters already includes both lower case and upper case letters
#lettersupc = letterslc.upper()
numbers = string.digits
punctuation = string.punctuation
allchars = letterslc+numbers+punctuation

lengthofpass = input("How many characters would you like your password to be?")
lengthofpass = int(lengthofpass)

password = "".join(random.sample(allchars,lengthofpass))
print(password)

You could also use either a for loop or a while loop and select a random character from the list of all characters at each iteration with the random.choice method.

import string
import random
print("---------Password Generator---------")

letterslc = string.ascii_letters
#There is no need of the command below since string.ascii_letters already includes both lower case and upper case letters
#lettersupc = letterslc.upper()
numbers = string.digits
punctuation = string.punctuation
allchars = letterslc+numbers+punctuation

lengthofpass = input("How many characters would you like your password to be?")
lengthofpass = int(lengthofpass)

password = ""

for _ in range(lengthofpass):
    password += random.choice(allchars)

print(password)
Daniel Ocando
  • 3,554
  • 2
  • 11
  • 19