-1

Somehow I can't manage to use a previously defined function inside another one. Is that possible?

def generate_password(length):
    import random
    password = str()
    alpha_num = [i for i in 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789']
    while len(password) <= length:
        symbol = random.choice(alpha_num)
        if symbol not in password:
            password += symbol
        else: 
            symbol = random.choice(alpha_num)
    return(password)
        

def generate_passwords(count, length):
    import random
    passlist = []
    for j in range(count):
        generate_password(length)
        if password not in passlist:
            passlist.append(password)
    print(passlist)        

n, m = int(input()), int(input())
generate_passwords(n, m)

this code returns an error:

 
`Traceback (most recent call last):
  File "jailed_code", line 24, in <module>
    generate_passwords(n, m)
  File "jailed_code", line 19, in generate_passwords
    if password not in passlist:
NameError: name 'password' is not defined`
7
  • 3
    Welcome to Stack Overflow. While you have certainly returned password in generate_password, you haven't assigned the return to a value. Commented Apr 26, 2023 at 7:58
  • I have upvoted this because it is a good question - it is clear what the problem is for someone who knows the answer (as ewokx demonstrates). Just a sidenote: Have you tried searching for similar questions? I'm sure there must be several already Commented Apr 26, 2023 at 7:59
  • You meant to write: password = generate_password(length) Commented Apr 26, 2023 at 8:03
  • @ewokx thank you kindly. I didn't realize that assigning the return is needed here! Commented Apr 26, 2023 at 8:10
  • 1
    @lucidbrot thanks as well. Yeah, I tried looking through similar questions but couldn't find what I needed. As ewokx has pointed out - it was a small and silly mistake on my side. Commented Apr 26, 2023 at 8:10

2 Answers 2

1

you're missing password = generate_password(length)

Also a set would be a much better data structure (pass_set instead of pass_list) for your operations.

import random
import string

def generate_password(length):
    alpha = list(string.ascii_letters + string.digits)
    return ''.join(random.sample(alpha, length))
        

def generate_passwords(count, length):
    pass_set = set()
    while(len(pass_set) < count):
        pass_set.add(generate_password(length))
    print(pass_set)        

generate_passwords(5, 10)
Sign up to request clarification or add additional context in comments.

Comments

0

You need to assign the returned value from the function call generate_password(length) into a variable (here, password) to be able to use it further.

def generate_password(length):
    import random
    password = str()
    alpha_num = [i for i in 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789']
    while len(password) <= length:
        symbol = random.choice(alpha_num)
        if symbol not in password:
            password += symbol
        else: 
            symbol = random.choice(alpha_num)
    return(password)
        

def generate_passwords(count, length):
    import random
    passlist = []
    for j in range(count):
        password = generate_password(length)
        if password not in passlist:
            passlist.append(password)
    print(passlist)        

n, m = int(input()), int(input())
generate_passwords(n, m)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.