Getting error when I run my program. What do I need to change?

Question:

      import random,  string

characters = string.ascii_letters + string.digits +     string.punctuation


def generate_random_password():
    length = int(input("How many characters in password? "))
    number_of_passwords = int(input("How many passwords would you     like? "))
    character_count = characters
    if character_count > str(length):
        print("To long, try again")
        return
    password = []
    for i in range(length):
        password.append(random.choice(characters))
        if character_count < str(length):
            random.shuffle(characters)
                for i in range(length - character_count):
                password.append(random.choice(characters))
        random.shuffle(password)
        output = "".join(password)
        print(output)
        return(output)

results = generate_random_password()

with open("pwd.txt", "w+") as file:
    file.write(results)

This the code for the generator and when I get to the end the result is the same:

    file.write(results)
TypeError: write() argument must be str, not None

I tried the str function and int function; I changed "w+" to "a". I thought using the str function would fix it, considering the argument must be str.

Asked By: Dustin Keith

||

Answers:

Code is now runnable. I edited it so that you can now generate a password of a specific length. Now, it’s up to you to complete the code so that it can generate more than 1 password.

import random, string

characters = list(string.ascii_letters + string.digits + string.punctuation)


def generate_random_password():
    length = int(input("How many characters in password?n"))
    number_of_passwords = int(input("How many passwords would you like?n"))
    character_count = len(characters)
    
    if length > character_count:
        print("Too long, try again")
        return -1

    password = []
    output = ""
    for i in range(length):
        output += "".join(random.sample(characters, length))
        password.append(output)
        print(output)
        return (output)


results = generate_random_password()

if results != -1:
    with open("pwd.txt", "w+") as file:
        file.write(results)
Answered By: Jack

When taking user input that needs to be of a certain type (in this case int) you should always validate it.

Functions should do what their name suggests – nothing more.

The random modules has a choices() function which is ideal for your needs.

from random import choices
from string import ascii_letters, digits, punctuation

CHARACTERS = ascii_letters + digits + punctuation

def generate_random_password(length):
    return ''.join(choices(CHARACTERS, k=length))

while True:
    try:
        if (length := int(input('How many characters in password? '))) > len(CHARACTERS):
            raise Exception(f'Maximum length is {len(CHARACTERS)}')
        password = generate_random_password(length)
        with open('pwd.txt', 'w') as pwd:
            pwd.write(password)
        break
    except Exception as e:
        print(e)
Answered By: DarkKnight