Peter
Peter

Reputation: 1

Can’t get random generated string to print itself

I am currently working on a custom made version of a random password generator. When I run my program, it creates the desired 11 character password, but then it prints a different password with various random characters right below it. How can I avoid this?

The desired outcome would be for it to generate a random 11 character password and then confirm it back to the user by printing it.

import string
import random

def generate():
    generated_pwd = string.ascii_letters
    print("Generating...")
    print(" ")
    print("Your randomly generated key is ")
    print(''.join(random.choice(generated_pwd) for i in range(11)))
    return generated_pwd

def add():
    username = input("What is usernme:")
    pwd = input("What is password: (for random, type 'generate') ")
    if pwd == 'generate' :
        generated_pwd = generate()
        print(generated_pwd)
        pwd = generated_pwd
        print("your password is " + pwd)
        return generated_pwd

Upvotes: 0

Views: 69

Answers (1)

MichaelCG8
MichaelCG8

Reputation: 579

generate() returns generated_pwd which is just str.ascii_letters. In generate() you print the random password but just return str.ascii_letters so that is what gets printed in add().

You want to put your random password in a variable, print the variable, and return the same variable. Change generate() like this:

def generate():
    print("Generating...\n")
    generated_pwd = "".join(random.choice(string.ascii_letters) for i in range(11))
    print(f"Your randomly generated key is {generated_pwd}")
    return generated_pwd

Upvotes: 1

Related Questions