Skip to content

Instantly share code, notes, and snippets.

@Limerin555
Last active May 26, 2017 12:39
Show Gist options
  • Save Limerin555/3578e123c63549099bed1d13f5bff73b to your computer and use it in GitHub Desktop.
Save Limerin555/3578e123c63549099bed1d13f5bff73b to your computer and use it in GitHub Desktop.
Attempting exercises from Practice Python( http://www.practicepython.org/ )
# Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password. Include your run-time code in a main method.
#
# Extra:
#
# Ask the user how strong they want their password to be. For weak passwords, pick a word or two from a list.
userin = input("Would you like a strong or weak password? ")
userin = userin.lower()
import string
import random
def generatePW():
"""
Evaluate if user wants a weak or strong password, before passing it respective password generator.
"""
if userin == "weak":
print(weakPW())
elif userin == "strong":
print(strongPW())
else:
print("Invalid input! Type 'Weak' or 'Strong' to generate a password.")
def weakPW(size = 6, chars = string.ascii_lowercase + string.digits):
"""
Will generate a weak password if user input matches.
"""
weak_pass = "".join(random.SystemRandom().choice(chars) for _ in range(size))
return "Your weak password is {}, please keep it with you at all times.".format(weak_pass)
def strongPW(size = 12, chars = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation):
"""
Will generate a strong password if user input matches.
"""
strong_pass = "".join(random.SystemRandom().choice(chars) for _ in range(size))
return "Your strong password is {}, please keep it with you at all times.".format(strong_pass)
generatePW()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment