我写了一个密码生成器,我遇到了这个恼人的问题,它是在同一行重复一个数字或字母。用户为程序提供了他们希望如何生成密码的格式,例如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)
答案 0 :(得分:0)
GenPass.replace(GenPass[Pos],number)
会将GenPass[Pos]
中的每个字符替换为number
的值。您需要确保一次替换一个字符。
答案 1 :(得分:0)
创建所有字符的列表和包含所有nums的列表,然后使用list.pop(randint(0,len(list) - 1)选择一个,你总是会选择一个不同的字母/数字,但是您还将被限制为10位数字(0-9)和20位字母。