Python Forum

Full Version: Trying to make a password generator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to make a password generator that restarts if you're not satisfied with your password. The generator itself works perfectly, but when I try to add an input function after the while loop (ques2) it doesn't work. How do I fix this?

import random

chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456!@#$%^&"

ques = input("Do you want a new password? ")

if ques=="yes":
    while 1:
        password_len = int(input("What lenght would you like your password to be : "))
        password_count = int(input("How many passwords would you like :"))
        for x in range (0,password_count):
            password = ""
            for x in range(0, password_len):
                password_char = random.choice(chars)
                password = password + password_char
            print("Here is your password: ", password)
            ques2 = input("Are you satisfied with your password? ")
else:
    print("Okay, goodbye")
Your problem is this:
while 1:
This loop will run forever. You need a way to break out. You also only ask this question once.
ques = input("Do you want a new password? ")
You should break your code up into two parts, one that makes the password, and one that manages the user interface. The password code makes one password. It verifies that the user likes the password, and returns the new password. The user interface is the part that asks the user how long the password should be, calls the function to make the password, and then asks the user if they want another password.

This leaves out the password generation part to focus on the user interface part.
def password(length):
    print("password")

while True:
    password(int(input("What lenght would you like your password to be : ")))
    if input("Do you want a new password? ").lower() != "yes":
        break
print("Okay, goodbye")
Usually password generators have rules such as "At least 1 upper and lower case letter" etc...