Skip to content

Instantly share code, notes, and snippets.

@timrprobocom
Created August 30, 2022 05:01
Show Gist options
  • Save timrprobocom/289400ca440fd365bdf4b2f6dc1584dc to your computer and use it in GitHub Desktop.
Save timrprobocom/289400ca440fd365bdf4b2f6dc1584dc to your computer and use it in GitHub Desktop.
"""
This does what you are trying to do, but with all the extraneous code eliminated.
Notice that strings are iterables, just like lists. Any time you are making a list
of single characters, you should probably use a string. Also notice that we write
`'xxx'.upper()`, not `str.upper('xxx')`. Also note that it is silly to write `
if result='N': pass`. Just test for what you want to know.
"""
import random
lower_case_letters = 'abcdefghijklmnopqrstuvwxyz'
upper_case_letters = lower_case_letters.upper()
numbers = "1234567890"
symbols = '!@#$%^&*()-_=+.,/:l"[{]`|'
result = ''
choice = input("Do you want lower case letters? [Y] for YES and [N] for NO").upper()
if choice == "Y":
result = result + lower_case_letters
choice = input("Do you want upper case letters? [Y] for YES and [N] for NO").upper()
if choice == "Y":
result = result + upper_case_letters
choice = input("Do you want numbers? [Y] for YES and [N] for NO").upper()
if choice == "Y":
result = result + numbers
choice = input("Do you want symbols? [Y] for YES and [N] for NO").upper()
if choice == "Y":
result = result + symbols
choice = input("Do you want to repeat characters? [Y] for YES and [N] for NO").upper()
length = int(input("length of password"))
password = []
if choice == "N":
while len(password) < length:
password.append(random.choice(result))
else:
result = random.shuffle(result)
password = result[:length]
print(''.join(password))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment