2017-10-19 4 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="") 

私の出力に割り当てる範囲のループ出力は見えのPython 3.0 - 次のように変数

How long should the password be?4 
GtRA 

私はパスと呼ばれる変数に、この出力を保存し、テキストファイル

にその変数を保存したいです

おかげ

+0

は、変数名として 'pass'を使用してはならない[' pass'](HTTPS://docs.python .org/3/reference/simple_stmts.html#pass)文 – DavidG

答えて

0

は私が輸入を最適化し、ファイルにパスワードを書いた:それはニシキヘビをシャドウとしてFYI

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

'PassLenInput'をキャストしてコードをもう少し整理できます入力と同じ行のint 'PassLenInput = int(input())' :) – DavidG

+0

@DavidG:そうです。私は答えにヒントを取り入れました。 – mrCarnivore

1

チェックアウト、この答え

#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

あなたは変数としてパスを使うべきではありません。 – utengr

+0

'pass'はPythonの特別なキーワードですので、変更しました。この場合、それは問題ではないと思った。 – Stack

+0

私はあなたがすでにそれを変更してはいけないと言っていました。 – utengr

関連する問題