我目前正在使用Python进行编码,我不知道为什么,我编写的程序无法按预期工作。基本上,我正在尝试制作一个密码生成器,但是在输入之后,它就会中断。
import string
import random
nspecialchr = int(input("Number special characters: "))
nnum = int(input("Number of Numbers in password: "))
NChr = int(input("Number of letters: "))
LSpecialChr = ['!', '§', '$', '%', '&', '/', '#']
FLSecialChr = []
while nspecialchr > 0:
FLSecialChr.append(LSpecialChr[random.randint(0, 6)])
nspecialchr -= 1
FSpecialChr = ''.join(FLSecialChr)
LNnum = []
while nnum > 0:
LNnum.append(random.randint(0, 9))
FNum = ''.join(LNnum)
LChr = []
while NChr > 0:
LChr.append(random.choice(string.ascii_letters))
NChr -= 1
FChr = ''.join(LChr)
print(FSpecialChr + FNum + FChr)
pasue = input()
我认为您在第二个循环的末尾错过了nnum -= 1
(因此您有一个无限循环)。最好写
for i in range(nnum):
LNnum.append(random.randint(0, 9))
您的代码有几个错误,在您的第二个while循环中,您错过了nnum -= 1
,我已修复它们,请尝试以下操作:
import string
import random
nspecialchr = int(input("Number special characters: "))
nnum = int(input("Number of Numbers in password: "))
NChr = int(input("Number of letters: "))
LSpecialChr = ['!', '§', '$', '%', '&', '/', '#']
FLSecialChr = []
while nspecialchr > 0:
FLSecialChr.append(LSpecialChr[random.randint(0, 6)])
nspecialchr -= 1
FSpecialChr = ''.join(FLSecialChr)
LNnum = ''
while nnum > 0:
LNnum+=str(random.randint(0, 9))
nnum -= 1
FNum = ''.join(LNnum)
LChr = []
while NChr > 0:
LChr.append(random.choice(string.ascii_letters))
NChr -= 1
FChr = ''.join(LChr)
print(FSpecialChr + FNum + FChr)
pasue = input()
输出:
Number special characters: 5
Number of Numbers in password: 4
Number of letters: 3
password generated is $#$%%1469Fsy