Ask coding questions

← Back to all posts
Password Generator
NextGen22Genera (1)

I'm trying to create a password generator and I want it to generate a password that has to include letters, numbers and symbols. But, when I ask for an input it would input (e.g 9) it would generate 9 letters, symbols and numbers. And if I just put all the letters, numbers and symbols in one string, it's not guaranteed to include symbols or numbers.
Help/Feedback is appreciated.
Thank you!

My Code:

heyitsmarcus (282)

So, since you have 3 different possible character sets to choose from, I made a simple r variable that makes a random choice between 1 and 3, and picks a character from a character set based on that choice.

I made a fork of your repl, with the modifications I listed above, you can look at:

import random
print ("""This is my simple password generator.""")
letters = "abcdefghijklmnopqrstuvwxyz"
symbols = "[email protected]#[email protected]*"
numbers = "123456789"
length = int(input("How long do you want your password? "))
password = ""
for c in range(length):
  r = random.randint(1,3)
  if (r == 1):
  	password += random.choice(letters)
  elif (r == 2):
	  password += random.choice(symbols)
  elif (r == 3):
	  password += random.choice(numbers)
print (password)
heyitsmarcus (282)

@NextGen22Genera You're welcome! Please upvote if my answer helped you, and good luck!

a5rocks (506)

Um it works for me... Just for c in length(length//3): so the password is the right length.

By the way, I recommend DiceWare as a password generator. It makes a memorable password.