2017-10-19 1 views
0
#Imports 
import string 
import random 
import time 


digits = string.digits 
letters = string.ascii_letters 
punctuation =("!\"\<>@#£$%^&*") 
PasswordCode = letters+punctuation+digits 

PassLenInput = input("How long should the password be?") 
PassLenInput = int(PassLenInput) 
for i in range(PassLenInput): 
    print(random.choice(PasswordCode),end="") 

Ma sortie se présente comme suitPython 3.0 - Attribuer sortie de la boucle de plage à la variable

How long should the password be?4 
GtRA 

Je voudrais enregistrer cette sortie à une variable appelée passe, puis enregistrez cette variable dans un fichier texte

Merci

+0

Pour votre information, vous ne devriez pas utiliser 'pass' comme nom de variable comme ombres pythons [' pass'] (https: //docs.python .org/3/reference/simple_stmts.html # pass) instruction – DavidG

Répondre

0

Je les importations et optimisé a écrit le mot de passe dans un fichier:

from string import digits 
from string import ascii_letters as letters 
import random 

punctuation =("!\"\<>@#£$%^&*") 
PasswordCode = letters+punctuation+digits 

PassLenInput = int(input("How long should the password be?")) 

password = '' 

for i in range(PassLenInput): 
    password += random.choice(PasswordCode) 

with open('password_file.txt', 'w') as f: 
    f.write(password) 
+0

Vous pouvez ranger le code un peu plus en lançant 'PassLenInput' à un int sur la même ligne que l'entrée par exemple 'PassLenInput = int (input())' :) – DavidG

+0

@DavidG: Vous avez raison. J'ai incorporé l'indice dans ma réponse. – mrCarnivore

1

Regardez cette réponse

#Imports 
import string 
import random 
import time 


digits = string.digits 
letters = string.ascii_letters 
punctuation =("!\"\<>@#£$%^&*") 
PasswordCode = letters+punctuation+digits 

PassLenInput = input("How long should the password be?") 
PassLenInput = int(PassLenInput) 
password = "" 
for i in range(PassLenInput): 
    password += random.choice(PasswordCode) 
    print(password) 

#Save Password 
with open("password.txt", "w") as save_password: 
    save_password.write(password) 
+0

vous ne devriez pas utiliser pass comme variable. – utengr

+0

'pass' est un mot-clé spécial dans python, donc l'a changé. Je pensais que ça n'avait pas d'importance dans ce cas. – Stack

+0

Je voulais dire que vous ne devriez pas :) Vous avez déjà changé. – utengr