Home > Software engineering >  Password Generator using randint instead of random.choice
Password Generator using randint instead of random.choice

Time:03-03

I tried to solve a mini quiz called Password Generator. The idea is to print out a random char by subsetting any characters from the list by specifying how many characters, symbols, and numbers. For example, I want 3 symbols in my password, hence it should subset 3 random characters from the symbol list.

I tried 2 ways:

The first one is by using random.choice to get random elements from list variables. It turns out successful.

The second attempt was to subset the element from the list by using the index, so I used random.randint for getting me a random index from a specific range. But my second attempt, instead of printing out different characters, gives me the same output.

Here's the code:

import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']

print("Welcome to the PyPassword Generator!")
nr_letters= int(input("How many letters would you like in your password?\n")) 
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))

password = ""


for l in range(0, nr_letters):
  password  = letters[random.randint(0,51)]

for s in range(0, nr_symbols):
  password  = symbols[random.randint(0,8)]

for n in range(0, nr_numbers):
  password  = numbers[random.randint(0,9)]

print(f"Your password is: {password}")

Here's the samples output from the above code(assume 2chars, 2symbols, 2numbers):

**1st Run:** cc@@44
**2nd Run:** vv!!00
**3rd Run:** ee((11

I'm expecting something like aw#*14 as the output or any random outputs from the code. Could anybody please point me to the mistake that I did? I'm new to programming and still trying to grasp the logic behind it. Much appreciate it! Thank you.

CodePudding user response:

Here's a rewrite without any user input and a simpler approach to the letters, digits and symbols.

What's important here is that duplication of pairs of letters, digits or symbols is unlikely - certainly not consistent as claimed by the OP

import random
import string

symbols = '!#$%&()* '

nr_letters = 2
nr_symbols = 2
nr_numbers = 2

for _ in range(10):
    password = ''
    for _ in range(nr_letters):
        password  = random.choice(string.ascii_letters)

    for _ in range(nr_symbols):
        password  = random.choice(string.digits)

    for _ in range(nr_numbers):
        password  = random.choice(symbols)

    print(f"Your password is: {password}")

Output:

Your password is: gX06*%
Your password is: VJ73#(
Your password is: Jn59* 
Your password is: pg67$&
Your password is: zI29%&
Your password is: Md35$&
Your password is: rc19! 
Your password is: ZL86!)
Your password is: Do04)%
Your password is: Qd76(&

CodePudding user response:

must be some optimization that affect randint. if you store it before it wont happens

import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']
print("Welcome to the PyPassword Generator!")
nr_letters= 2 #int(input("How many letters would you like in your password?\n")) 
nr_symbols = 2 #int(input(f"How many symbols would you like?\n"))
nr_numbers = 2 #int(input(f"How many numbers would you like?\n"))
password = ""
for l in range(0, nr_letters):
  a = random.randint(0,51)
  password  = letters[random.randint(0,51)]
for s in range(0, nr_symbols):
  a=random.randint(0,8)  
  password  = symbols[random.randint(0,8)]
for n in range(0, nr_numbers):
  a=random.randint(0, 9)
  password  = numbers[a]
  • Related