Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 写入文本文档时,它不会';不要给出所有给定的密码_Python_Python 3.x - Fatal编程技术网

Python 写入文本文档时,它不会';不要给出所有给定的密码

Python 写入文本文档时,它不会';不要给出所有给定的密码,python,python-3.x,Python,Python 3.x,我制作了一个密码生成器,根据请求的数量和长度为我提供密码,我想将所有给定的密码保存到一个名为“Your_saved_Keys”的txt文档中,但是只保存一个生成的密码,而不是所有密码 import random import time print(''' Password Generator V2.0 ======================= ''') chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@£$%^

我制作了一个密码生成器,根据请求的数量和长度为我提供密码,我想将所有给定的密码保存到一个名为“Your_saved_Keys”的txt文档中,但是只保存一个生成的密码,而不是所有密码

import random
import time

print('''
Password Generator V2.0
=======================
''')

chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@£$%^&*().,? 
0123456789'

number = input('number of passwords? ')
number = int(number)

length = input('password length? ')
length = int(length)

print('''\nhere are your passwords: ''')

for pwd in range(number):
  password = ''''''
  for c in range(length):
    password += random.choice(chars)
  print(password)

save = input("""Do you want to save it to a txt file? Y/N""")

if save == "Y":
  format = ".txt"
  title = "Your_Saved_Keys"
  text_file = open(title + format, "w")
  text_file.write(password))
  print("Save Successful")

if save == "N":
  print("You Selected No")
print("-----------------------------------")
input("Press enter to exit")

您请求保存,并且在完成整个
range(number)
循环后进行保存。因此,当然只保存最后生成的密码


循环前询问并保存循环中的每个密码,或将所有密码保存在列表中,然后保存列表。

您正在将
密码
变量写入文件<代码中的code>password变量将最后生成的密码存储在循环中

所以,为了实现你想要的

将生成的密码存储在列表中。(在第一个循环中,将生成的每个密码添加到此列表中)

然后将此列表的内容写入您的文件


注意:我建议您对文件应用加密

您的密码变量每次都被覆盖。只有最后一个密码可用。您可以将所有密码保存到列表中,然后将其写入文件。此代码正在运行

import random
import time

print('''
Password Generator V2.0
=======================
''')

chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@£$%^&*().,?0123456789'

number = input('number of passwords? ')
number = int(number)

length = input('password length? ')
length = int(length)

passwords=[]
print('''\nhere are your passwords: ''')

for pwd in range(number):
  password=""
  for c in range(length):
      password+=random.choice(chars)
  passwords.append(password)
  print(password)

save = input("""Do you want to save it to a txt file? Y/N""")

if save == "Y":
  format = ".txt"
  title = "Your_Saved_Keys"
  with open(title + format, "w") as text_file:
      for password in passwords:
          text_file.write(password+'\n')
  print("Save Successful")

if save == "N":
  print("You Selected No")
print("-----------------------------------")
input("Press enter to exit")

我打算以后再做