Home > database >  How to Loop a Function in Python Without a Clear Variable
How to Loop a Function in Python Without a Clear Variable

Time:02-04

Below is code that correctly outputs a random password based on the sequence, using multiple for loops. I want to loop the function 10 times to output 10 passwords, but I can't seem to figure out what variable to loop in this case.

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!")
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = (10 - num_letters - num_numbers)
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
password_list = []
for num in range(num_letters):
`letter = random.choice(letters)`    
`password_list.append(letter)`
for num in range(num_numbers):
`number = random.choice(numbers)`
`password_list.append(number)`
for num in range(num_symbols):
`symbol = random.choice(symbols)`
`password_list.append(symbol)`
random.shuffle(password_list)
password = ''   

for char in password_list:
`password = password   char`

print(password)

CodePudding user response:

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 = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']

def generator():
    print("Welcome to the PyPassword Generator!")
    num_letters= random.randint(3, 6)
    num_numbers = random.randint(1, 4)
    num_symbols = (10 - num_letters - num_numbers)
    print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
    password_list = []
    for num in range(num_letters):
        letter = random.choice(letters)
        password_list.append(letter)
    for num in range(num_numbers):
        number = random.choice(numbers)
        password_list.append(number)
    for num in range(num_symbols):
        symbol = random.choice(symbols)
        password_list.append(symbol)
    random.shuffle(password_list)
    password = ''   

    for char in password_list:
        password = password   char
    print(password)
    return password

print([generator() for i in range(10)])

CodePudding user response:

Just put this generator in a function!

As such I have a main function to run the program and ask how many times you would like to iterate the password generator function.

import random

def main():

    print("Welcome to the PyPassword Generator!")
    val = input("Enter number of passwords you want to generate: ")
    itterate = int(val)
    #print("Generated passwords: ")

    for i in range(itterate):
        passwordGenerator()

def passwordGenerator():
    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 = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']
    
    num_letters= random.randint(3, 6)
    num_numbers = random.randint(1, 4)
    num_symbols = ((10 - num_letters - num_numbers)   1) 
    print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')
    password_list = []
    for num in range(num_letters):
        letter = random.choice(letters)
        password_list.append(letter)

    for num in range(num_numbers):
        number = random.choice(numbers)
        password_list.append(number)
    for num in range(num_symbols):
        symbol = random.choice(symbols)
        password_list.append(symbol)
        random.shuffle(password_list)
        password = ''   

    for char in password_list:
        password = password   char

    print(password)

if __name__ == "__main__":
    main()

Sample output:

Welcome to the PyPassword Generator!
Enter number of passwords you want to generate: 3
Your requested 3 letters, 3 numbers, and 5 symbols:
(7I3$7&%)vm
Your requested 4 letters, 1 numbers, and 6 symbols:
&*PV#6u#N&)
Your requested 3 letters, 2 numbers, and 6 symbols:
8#*)8!!lt)h

CodePudding user response:

  • I put all your loops in the function to make your code easy to read and undrestand
  • no_passwords = 10 i make new variable to specify the number of passwords you want
  • for _ in range(no_passwords): generat_password() make loop run in range of the variable we made called no_passwords and each time the code will call the function generat_password() to generate a new password
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 = ['!', '#', '$', '%', '&', '(', ')', '*', ' ']

def generat_password():
    password_list = []

    for num in range(num_letters):
        letter = random.choice(letters)
        password_list.append(letter)

    for num in range(num_numbers):
        number = random.choice(numbers)
        password_list.append(number)

    for num in range(num_symbols):
        symbol = random.choice(symbols)
        password_list.append(symbol)

    random.shuffle(password_list)
    password = ''

    for char in password_list:
        password = password   char

    print(password)


print("Welcome to the PyPassword Generator!")
num_letters= random.randint(3, 6)
num_numbers = random.randint(1, 4)
num_symbols = (10 - num_letters - num_numbers)
no_passwords = 10
print(f'Your requested {num_letters} letters, {num_numbers} numbers, and {num_symbols} symbols:')

for _ in range(no_passwords):
    generat_password()
  • Related