Python Forum
Random Password Generator with Weird Output. Why?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Random Password Generator with Weird Output. Why?
#1
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?
Reply
#2
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.
Reply
#3
That worked! I changed the names of the variables. Thank you for the explanation too, that makes total sense!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unexpected output while using random.randint with def terickson2367 1 504 Oct-24-2023, 05:56 AM
Last Post: buran
  Trying to make a password generator Jankofcik 1 534 Aug-06-2023, 01:22 PM
Last Post: deanhystad
  Random coordinate generator speed improvement saidc 0 2,047 Aug-01-2021, 11:09 PM
Last Post: saidc
  Yield generator weird output Vidar567 8 3,257 Nov-23-2020, 10:59 PM
Last Post: deanhystad
  Random number generator charlottelol 5 3,196 Nov-10-2020, 10:51 PM
Last Post: deanhystad
  basic random number generator in replace function krug123 2 2,029 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Password output swittler 5 2,229 Jun-21-2020, 06:01 AM
Last Post: Gribouillis
  Help with a random number generator dpcalder 2 2,321 Jun-20-2020, 03:50 AM
Last Post: buran
  how to output to stderr from inside a generator? Skaperen 2 1,815 Mar-03-2020, 03:17 AM
Last Post: Skaperen
  output a list of random numbers 'x' columns wide adityavpratap 4 2,966 Jan-13-2020, 05:32 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020