Home > OS >  How to clear a string upon regeneration?
How to clear a string upon regeneration?

Time:07-19

I am very new to Python & Stack Overflow so apologies if I misjudge or misidentify the issue.

I have a programmed a password generator (code below) and I have made an input for how many passwords you'd like to generate and how many characters for each.

When filling out the credentials, I have realized that it works correctly for the amount of passwords and the characters, but it prints the same password. I think I understand that the issue is that it prints the 'passwords' string which was previously generated in the for loop above, but I am unaware on how to make it print a different string for each number of passwords requested.

Here is my code:

import random
import os

clear = ('cls' if os.name == 'nt' else 'clear')

print("Welcome to Your Password Generator!")

passwords = ""

characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@£$%&().,^"

number = input("> How many passwords do you wish to generate? ")
number = int(number)

pwlength = input("> How many characters should your password be? ")
pwlength = int(pwlength)

os.system(clear)

print("> Here are your generated passwords: \n ")

for pwamount in range(pwlength):
    passwords  = random.choice(characters)
for characterlength in range(number):
    print(passwords)

I apologize if the title or details are not descriptive as I am new to all this as a whole, If anybody has questions please give a response and I will try to respond with as much details as I can!

Thank you!

CodePudding user response:

Your loops are wrong.

Just check this out and try it

import random
import os

clear = ('cls' if os.name == 'nt' else 'clear')

print("Welcome to Your Password Generator!")

characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@£$%&().,^"

number = input("> How many passwords do you wish to generate? ")
number = int(number)

pwlength = input("> How many characters should your password be? ")
pwlength = int(pwlength)

os.system(clear)

print("> Here are your generated passwords: \n ")

for characterlength in range(number):
    print("".join(map(str, random.sample(characters,pwlength))))

CodePudding user response:

The problem is your code generates the password only once. I added an outer for loop to generate passwords corresponding to the number.

import random
import os

clear = ('cls' if os.name == 'nt' else 'clear')

print("Welcome to Your Password Generator!")

passwords = []

characters = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@£$%&().,^")

number = input("> How many passwords do you wish to generate? ")
number = int(number)

pwlength = input("> How many characters should your password be? ")
pwlength = int(pwlength)

os.system(clear)

print("> Here are your generated passwords: \n ")

for num_pw in range(number):
    password = ""
    for pwamount in range(pwlength):
        password  = random.choice(characters)
    passwords.append(password)

for password in passwords:
    print(password)

Output:

Welcome to Your Password Generator!
> How many passwords do you wish to generate? 3
> How many characters should your password be? 5
> Here are your generated passwords: 
 
AqlY(
b!dHG
$Q1dk

CodePudding user response:

I believe that your problem is not setting the variable password to a blank string and then making a new password. I would strongly recommend using lists, in this case

import random
import os

clear = ('cls' if os.name == 'nt' else 'clear')

print("Welcome to Your Password Generator!")

passwords = ""

characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789!@£$%&().,^"

number = input("> How many passwords do you wish to generate? ")
number = int(number)

pwlength = input("> How many characters should your password be? ")
pwlength = int(pwlength)

os.system(clear)

print("> Here are your generated passwords: \n ")
global pwlist
pwlist = []
for i in range(number):
  for pwamount in range(pwlength):
    passwords  = random.choice(characters)
  pwlist.append(passwords)
  passwords = ""
for password in pwlist:
  print(password)

something like this should work

CodePudding user response:

Your for loop is only generating one password at a time. Try something along these lines:

for password_num in range(number):
    password = ""
    for pwamount in range(pwlength):
        password  = random.choice(characters)
    print(password)

The difference is that you want to generate and print number passwords. For this reason, the code to execute and print should execute number times, and that should be your outer loop. Then, for each password, you need to generate a password of length pwlength, then print it.

Right now, your passwords variable is only a string, not a list of strings.

  • Related