【发布时间】:2021-04-08 13:27:53
【问题描述】:
我是编程新手,我正在做一个密码管理器。 我的问题:-它的作用->当我运行程序并输入密码的长度时,它会生成一个密码,但它始终是 4 位数字,并且密码重复的次数与我输入的数字一样多。 - 它应该做什么 -> 我输入的数字应该决定密码的长度,而不是重复多少次。
import random
#shuffle the list
def shuffle(string):
tempList = list(string)
random.shuffle(tempList)
return ''.join(tempList)
#the password functions
uppercaseLetter=chr(random.randint(65,90))
lowercaseLetter=chr(random.randint(97,122))
punctuationSign=chr(random.randint(32,152))
digit=chr(random.randint(48,57))
#completing the password
passwordLength = int(input("choose your password length: "))
possibleChars = uppercaseLetter, lowercaseLetter, punctuationSign, digit
ranChar = shuffle(possibleChars)
tempPassword = []
count = 0
while count != passwordLength:
tempPassword.extend(ranChar)
count = count + 1
password = ''.join(tempPassword)
#for which sitename
sitename = input('Save under which name: ')
print (password, sitename)
data=open("test.txt",'a')
data.write(sitename +' ')
data.write(password +'\n')
data.close()
【问题讨论】:
-
请说明您希望它做什么与它实际在做什么。还可以尝试在程序中添加
print()调用来宣布某些变量的值,以帮助您确保它们是您认为的那样。 -
不需要计数器。此外,如果您正在生成随机密码,则密码的长度最多为 4,即您定义的元组中的值的数量。在这种情况下,大写、小写等会生成一个随机值。
标签: python python-3.x random password-manager