2016-07-28 45 views
1

我写了一个密码生成器,我遇到了这个烦人的问题,它是在同一行重复一个数字或字母。用户给程序一个格式,说明他们希望如何生成他们的密码,例如“C @@ d %%%” 其中@只是字母,而%只是数字,用户还输入数字和字母以生成密码,那么该程序是假设打印出像cold123这样的东西,而是打印出cood111或clld111,我会在下面发布我的代码片段,但请不要说坏话,我对python相当陌生,自学成才,几个月后进入蟒蛇体验。如何阻止我的程序生成相同的密钥?

class G() 
    . 
    . 
    . 

    # self.forms is the format the user input they can input things such as [email protected]@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) 
+1

这是一个练习,对不对? – l0b0

+0

我认为问题是'replace()'函数我相信如果你传递第三个参数',1',那么只有第一个匹配项会被替换,而不是所有的匹配项 – depperm

回答

0

GenPass.replace(GenPass[Pos],number)将与number值替换在GenPass[Pos]出现的字符。您需要确保一次替换一个角色。

+0

以替换一个,你可以简单地添加第三个参数到'替换'像'GenPass.replace(GenPass [Pos],number,1)'',只有第一个将被替换 – depperm

+0

怎么样?替换的东西全部取代 – user5117999

+0

所以基本上GenPass.replace(GenPass [Pos],number,int(Pos)) – user5117999

0

创建一个包含所有num的所有字符和列表的列表,然后通过使用list.pop(randint(0,len(list)-1)选择一个列表,您将始终选择一个不同的字母/数字,像这样但你也会被限制在10位数字(0-9)和20个字母之内。

+0

我不关注。 – user5117999

+0

从列表中弹出是一个可怕的想法 - 你正在减少密码的熵。 – l0b0

+0

是否可以在不减少熵的情况下重复字符? – rodjun