Python 输入isn';t选择羊草的长度
我是编程新手,我正在做一个密码管理器。 我的问题:-它做什么->当我运行程序并输入密码长度时,它会生成一个密码,但它总是4位数长,密码重复次数与我输入的数字相同。 -它应该做什么->我输入的数字应该决定密码的长度,而不是密码被重复多少次Python 输入isn';t选择羊草的长度,python,python-3.x,random,password-manager,Python,Python 3.x,Random,Password Manager,我是编程新手,我正在做一个密码管理器。 我的问题:-它做什么->当我运行程序并输入密码长度时,它会生成一个密码,但它总是4位数长,密码重复次数与我输入的数字相同。 -它应该做什么->我输入的数字应该决定密码的长度,而不是密码被重复多少次 import random #shuffle the list def shuffle(string): tempList = list(string) random.shuffle(tempList) return ''.join(tempLis
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()
我认为你真的把你需要做的事情复杂化了,就我所知,以下是你试图做的事情
import random
#completing the password
passwordLength = int(input("choose your password length: "))
temp_pass = ''
count = 0
while count != passwordLength:
char_type = random.randint(0,3)
if char_type == 0:
random_char = chr(random.randint(65,90))
elif char_type == 1:
random_char = chr(random.randint(97,122))
elif char_type == 2:
random_char = chr(random.randint(32,152))
else:
random_char = chr(random.randint(48,57))
temp_pass = temp_pass + random_char
count = count + 1
print(temp_pass)
希望这有帮助。我建议在尝试这样的事情之前多练习一些基础知识,代码中有很多不好的练习。请澄清您希望它做什么,以及它实际在做什么。还可以尝试添加
print()
调用,以在程序中宣布某些变量的值,以帮助您确保它们是您认为的值。不需要计数器。此外,如果要生成随机密码,密码的长度将为atmost 4,即定义的元组中的值数。在这种情况下,大写、小写等将生成一个随机值。这里也有一些不好的做法(特别是在循环中),我们不能确定,但我怀疑密码不应该只包含一到四个唯一字符,不管密码长度如何。您好,是的,我刚刚注意到错误并修复了它,而且,这些坏习惯来自我复制的代码,因此我才声明它