python 中的密码生成程序
Password Generating program in python
我生成了随机密码。现在我想将每个密码分配给用户列表中的 each/different 个用户。但只有最后一个密码被分配给每个用户。代码如下。
import random
chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456!@#$%^&*()"
users=['A','B','C']
users_count= 0
for i in users:
users_count = users_count + 1
# print (str(users_count))
pass_len=int(input("Password length = "))
pass_count=users_count
for i in range(0,pass_count):
password=""
for i in range(0,pass_len):
pass_chars= random.choice(chars)
password=password+pass_chars
print("all pass are : " ,password)
for user in users:
print(user," Password : ",password)
输出是这样的:
Password length = 5
all pass are : As6iI
all pass are : FhAIX
all pass are : fjJBH
A Password : fjJBH
B Password : fjJBH
C Password : fjJBH
'password'是一个变量,一次只能有1个值,当你在循环中更改密码的值时,当循环结束时,只保留最后一个值。
创建一个数组并在循环中将密码附加到它,然后在循环中为每个用户调用该数组。
使用字典存储对 。
user_passwords = {}
for user in users: # For each user
password = ""
for i in range(0, pass_len): # Lets generate the password
password = password + random.choice(chars) # Add a random character
user_passwords[user] = password # assign the password to the user
for user in users:
print(user," Password : ",user_passwords[user])
或更短的版本:
user_passwords = {}
for user in users:
for i in range(0, pass_len):
user_passwords[user] = user_passwords.get(user, "") + random.choice(chars)
for user in users:
print(user," Password : ",user_passwords[user])
您的代码问题:
password=""
for i in range(0,pass_len):
pass_chars= random.choice(chars)
password=password+pass_chars
print("all pass are : " ,password)
# <-- Missing assigning the password to the user
是您生成了随机密码但没有将它们分配给用户。
but only the last password is being assigned to each user. The code is
below.
那是因为您通过始终打印之前生成的最后一个密码来遍历用户:
for user in users:
print(user," Password : ",password)
我生成了随机密码。现在我想将每个密码分配给用户列表中的 each/different 个用户。但只有最后一个密码被分配给每个用户。代码如下。
import random
chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456!@#$%^&*()"
users=['A','B','C']
users_count= 0
for i in users:
users_count = users_count + 1
# print (str(users_count))
pass_len=int(input("Password length = "))
pass_count=users_count
for i in range(0,pass_count):
password=""
for i in range(0,pass_len):
pass_chars= random.choice(chars)
password=password+pass_chars
print("all pass are : " ,password)
for user in users:
print(user," Password : ",password)
输出是这样的:
Password length = 5
all pass are : As6iI
all pass are : FhAIX
all pass are : fjJBH
A Password : fjJBH
B Password : fjJBH
C Password : fjJBH
'password'是一个变量,一次只能有1个值,当你在循环中更改密码的值时,当循环结束时,只保留最后一个值。
创建一个数组并在循环中将密码附加到它,然后在循环中为每个用户调用该数组。
使用字典存储对
user_passwords = {}
for user in users: # For each user
password = ""
for i in range(0, pass_len): # Lets generate the password
password = password + random.choice(chars) # Add a random character
user_passwords[user] = password # assign the password to the user
for user in users:
print(user," Password : ",user_passwords[user])
或更短的版本:
user_passwords = {}
for user in users:
for i in range(0, pass_len):
user_passwords[user] = user_passwords.get(user, "") + random.choice(chars)
for user in users:
print(user," Password : ",user_passwords[user])
您的代码问题:
password=""
for i in range(0,pass_len):
pass_chars= random.choice(chars)
password=password+pass_chars
print("all pass are : " ,password)
# <-- Missing assigning the password to the user
是您生成了随机密码但没有将它们分配给用户。
but only the last password is being assigned to each user. The code is below.
那是因为您通过始终打印之前生成的最后一个密码来遍历用户:
for user in users:
print(user," Password : ",password)