Python Random Password Generator adding onto previous password in while loop instead of creating a new password

Viewed 23

My Python Random Password Generator that is in a while loop doesn't create a new password when prompted to run again, but rather adds onto the previous password? If I run the entire program again after ending it, a new password will appear. Is there a way for me to have it give a new password every time instead of just adding on the prompted amount of characters to the previous one?

import random
import string

lowercase = [string.ascii_lowercase]
uppercase = [string.ascii_uppercase]
number = [string.digits]
symbols = [string.punctuation]
password_outputs = string.ascii_lowercase + string.ascii_uppercase + string.digits +string.punctuation

print("Welcome to the RPG!")

gen_password=''
stop = False
while not stop:
    num_char = int(input('Enter in the ammount of characters for the desired password: '))
    while num_char > 0:
        rand_char = random.choice(password_outputs)
        gen_password += rand_char
        num_char -= 1
            #basically a redstone repeater that decreases in value until it hits 0 and can contiue. User able to pick start value
        if num_char == 0:
            print(gen_password)
            #ensures that only the final product of while loop is printed
    #if num_char != int:
        #stop = False
        #want to make it more secure against non-integer inputs, not sure how to go about this

    user_continue = input('Would you like to generate a longer password? (y/n): ')
    if user_continue == 'y':
        stop = False
    elif user_continue == 'n':
        stop = True
    else:
        print('Invalid Operation, running generator again')
        stop = False

print("Have a nice day!")

For example, if I input y and run the while loop again, it will just add the inputed amount of characters onto the previous password to create a longer password. Any help is appreciated.

1 Answers

Done some changes in your code

import random
import string

lowercase = [string.ascii_lowercase]
uppercase = [string.ascii_uppercase]
number = [string.digits]
symbols = [string.punctuation]
password_outputs = string.ascii_lowercase + string.ascii_uppercase + string.digits +string.punctuation

print("Welcome to the RPG!")

gen_password=''
stop = False
num_char = 0. # include this line if you want that earlier user entered 3 and in next iteration 5 so total you want 8 character password if you only want 5 character password then you can remove this line
while not stop:
    gen_password='' # each time it will default to empty 
    num_char += int(input('Enter in the ammount of characters for the desired password: '))
    while num_char > 0:
        rand_char = random.choice(password_outputs)
        gen_password += rand_char
        num_char -= 1
            #basically a redstone repeater that decreases in value until it hits 0 and can contiue. User able to pick start value
        if num_char == 0:
            print(gen_password)
            #ensures that only the final product of while loop is printed
    #if num_char != int:
        #stop = False
        #want to make it more secure against non-integer inputs, not sure how to go about this

    user_continue = input('Would you like to generate a longer password? (y/n): ')
    if user_continue == 'y':
        stop = False
    elif user_continue == 'n':
        stop = True
    else:
        print('Invalid Operation, running generator again')
        stop = False

print("Have a nice day!")
Related