-1

这是一个简单的程序,它会提示输入密码的长度以及要创建多少个密码。我需要将结果打印到文件中。但是,它将所有结果打印到一行中。见下文。

这是代码:

import string
import random
print('''---Password Generator---''')
characters = string.punctuation + string.ascii_letters + string.digits
numofpasswd = int(input('How many passwords would you like?: '))
passwdlength = int(input('Please pick amount of characters. Pick more than 8 characters for better security: '))
if passwdlength < 8:
    print("Password is less than 8 characters. Please restart program.")
else:
    for password in range(numofpasswd):
    passwd = ''
    for char in range(passwdlength):
        passwd += random.choice(characters)
    print(passwd)
    f = open("pass.txt", 'a')
    f.write(passwd)
    f = open('pass.txt', 'r')
    f.close()

这是一个示例输出。我请求了 2 个长度为 9 的密码:

~Lf>8ohcY

Q* tPR :,

这是写入 pass.txt 的内容:

~Lf>8ohcYQ* tPR :,

如您所见,它结合了输出。请帮忙。

额外:有没有办法简化这段代码?谢谢!

4

1 回答 1

0

在每个密码后写一个换行符:

f.write(passwd + '\n')

另外,你不应该这样做

f = open('pass.txt', 'r')

f.close()
于 2020-06-10T00:55:11.557 回答