我写了一个密码生成器,遇到了这个烦人的问题,它是在同一行重复一个数字或字母。用户为程序提供了一种格式,说明他们希望如何生成密码,例如"C@@d%%%"其中@只有字母,%只有数字,用户还输入数字和字母来生成密码,那么程序应该打印出类似cold123的东西,但它打印出cood111或clld111,我会在下面发布我的代码片段,但请不要说脏话,我对python还很陌生,自学成才,仅仅几个月的python经验。
class G()
.
.
.
# self.forms is the format the user input they can input things such as C@@d%%%
# where @ is only letters and where % is only numbers
# self.Bank is a list where generated things go
AlphaB = [] #list Of All Of The Positions That have The @ sign in The self.forms
NumB = [] #list of All of the positions that have a % sign
for char in self.forms:
if char == '@':
EOL=(self.Position) # Positions End Of Line
Loc = self.forms[EOL] # Letter
AlphaB.append(EOL)
if char == '%':
EOL=(self.Position)
Loc = self.forms[EOL]
NumB.append(EOL)
self.Position+=1 # Move right a position
for pos in AlphaB:
for letter in self.alphas: #letters in The User Inputs
GenPass=(self.forms.replace(self.forms[pos],letter))
#Not Fully Formatted yet, because Only The letter been formatted
if GenPass.find('%'):
for Pos in NumB:
for number in self.ints:
GenPass=(GenPass.replace(GenPass[Pos],number))
if GenPass not in self.Bank:
#Cood111
print (GenPass)
self.Bank.append(GenPass)
else:
if GenPass not in self.Bank:
print (GenPass)
self.Bank.append(GenPass)
GenPass.replace(GenPass[Pos],number)
将用number
的值替换字符在GenPass[Pos]
处每次出现的。您需要确保一次替换一个字符。
创建一个所有字符的列表和一个包含所有数字的列表,然后使用list.pop(randint(0,len(list)-1)选择一个,你总是会像这样选择一个不同的字母/数字,但你也会被限制为10位数字(0-9)和20多个字母。