Skip to main content

Random password generator program using python.

Random password generator

Write a programme, which generates a random password for the user. Ask the user how long they want their password to be, and how many letters and numbers they want in their password. Have a mix of upper and lowercase letters, as well as numbers and symbols. The password should be a minimum of 6 characters long.





import string
import random
import sys

# Taking Password Length
pass_len = int(input("Enter Password Length (Min. 6 & Max. 8 ): "))

# Checking for password is in range or not
if pass_len < 6 or pass_len > 8:
    print("Invalid password length!!, Min. 6 & Max. 8 Characters Allowed")
    sys.exit(0)

# Password's letters count
letters_len = int(input("How many letters do you want? (Max. {} & Min. 0): ".format(pass_len)))

# Password's numbers count
num_len = int(input("How many numbers you want? (Max. {} & Min. 0): ".format(pass_len-letters_len)))

# Checking for valid inputs
if pass_len < (letters_len+num_len):
    print("Invalid inputs!! Letters length plus numbers length exceed total password length.")
    sys.exit(0)
sp_sym = 'Y'
if pass_len != (letters_len+num_len):
    sp_sym = input("Include any special symbol? (Y for Yes, N for No): ")

# Checking for valid character
if sp_sym not in ['Y','y','N','n']:
    print("Wrong Character!! Y/y for Yes and N/n for No ")
    sys.exit(0)

# Password without special symbols
if sp_sym == 'N' or sp_sym == 'n':
    print("Invalid inputs!! Letters length plus numbers length is not equal to password length.")
    sys.exit(0)

# Password with special symbols
if sp_sym == 'Y' or sp_sym == 'y':
    passw = random.sample(string.ascii_letters,letters_len) + random.sample(string.digits,num_len) \
             + random.sample(string.punctuation,pass_len-(letters_len+num_len))
else:
    passw = random.sample(string.ascii_letters+string.digits,pass_len)

# Shuffle random character's list
random.shuffle(passw)

# List to string conversion
pass_gen = ''.join(passw)

print("Your Password is generated: ",pass_gen)

Output

Enter Password Length (Min. 6 & Max. 8 ): 8
How many letters do you want? (Max. 8 & Min. 0): 4
How many numbers you want? (Max. 4 & Min. 0): 1
Include any special symbol? (Y for Yes, N for No): y
Your Password is generated:  !sS]wv9.


Comments

Popular posts from this blog

Why we should learn python ?

Why we should learn Python? Python is a general purpose programming language. Created nearly 30 years ago, it is now one of the most popular language.Its popularity is particularly important in the data science and machine learning fields. But it is also a language easy to learn, this explains why it has become the language the most taught in universities. Python interpreters are available for the main operating systems (Linux, Mac OS, Windows, Android, iOS, BSD, etc.). Python is easy to learn Python has a simple syntax that makes it suitable for learning programming as a first language. The learning curve is smoother than other languages such as Java, which quickly requires learning about Object Oriented Programming or C/C++ that requires to understand pointers. Still, it's possible to learn about OOP or functional programming in Python when the time comes. Python provides a well-furnished standard library and many external libraries are available. This allows to qu...

assignment

import random n= random.randint(1, 99) guess = int(input("Enter an integer from 1 to 99: ")) while (n != "guess"):     print     if guess<n:         print ("guess is low")         guess=int(input("Enter an integer from 1 to 99: "))     elif guess>n:         print ("guess is high")         guess = int(input("Enter an integer from 1 to 99: "))     else:             print ("you guessed it!")             break             print