Python Forum

Full Version: Random Password Generator with Weird Output. Why?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone! I heard the Python community was great and I'm just getting started on my journey. I created a random password generator with the help of a beginner Python book. The program is supposed to generate three unique passwords, incorporating one word from each of the lists I've provided (chosen at random), a random number between 100 and 999, and a special character.
It does work for the first password generated, but for the next two passwords instead of incorporating a full word from each list, it just takes one letter. Super weird. It does this for every next password as well if you input 'yes' when the program asks if you want different passwords.
Anyone have any idea why it does this? Below is my code, and the last output I got:

import random
import string

random_list1 = ['space', 'radiate', 'nine', 'answer', 'educate', 'entertaining', 'range', 'teeny', 'watery', 'impress',
                'tease', 'subtract', 'position', 'loyalty', 'sticky', 'dividend', 'advocate', 'enter', 'hill',
                'craftsman', 'hike', 'farewell']

random_list2 = ['halt', 'flourish', 'officer', 'hospitality', 'operation', 'cart', 'relative', 'insist', 'epic',
                'wind', 'cheat', 'dividend', 'cooperate', 'get', 'bell', 'horror', 'rotation', 'cap', 'grip', 'roof']

print("Welcome to the Password Generator! I've created three randomly generated, strong passwords for you below :)")
generating = True

while generating:

    for num in range(3):

        random_list1 = random.choice(random_list1)
        random_list2 = random.choice(random_list2)

        number = random.randrange(100, 999)

        special_char = random.choice(string.punctuation)
        special_char2 = random.choice(string.punctuation)

        password = random_list1 + str(number) + special_char + random_list2 + special_char2
        print("\nYour new password is: " + password + "\n")

    not_yes_or_no = True
    while not_yes_or_no:
        response = input("Want different passwords?\n")
        response = response.lower()
        if response == "no":
            not_yes_or_no = False
            generating = False
        elif response == "yes":
            not_yes_or_no = False
            generating = True
        elif response != "yes" or response != "no":
            print("\nPlease just type 'Yes' or 'No'...\n")


print("\nOkay! Make sure you don't forget your new password!")
Output:
Welcome to the Password Generator! I've created three randomly generated, strong passwords for you below :) Your new password is: farewell356:officer< Your new password is: f614-f~ Your new password is: f481{f} Want different passwords? yes Your new password is: f281[f) Your new password is: f679-f* Your new password is: f821+f\ Want different passwords?
The problem is in Line 18 and Line 19. random_choice(random_list1) will return a element from the list. So with random_list1 = random.choice(random_list1), You have changed the random_list1 from list to string (one element in list). The next time you call random.choice(random_list1), it will choose a character from the string. And the next time random_list1 only contains one letter.
Choose a different variable name will help.
That worked! I changed the names of the variables. Thank you for the explanation too, that makes total sense!