简体   繁体   English

如何阻止我的程序生成相同的密钥?

[英]How can I stop my program from generating the same key?

I writing a password generator and I ran into this annoying issue, and it is the repeating aa number or letter on the same line. 我写了一个密码生成器,我遇到了这个恼人的问题,它是在同一行重复一个数字或字母。 The user gives the program a format on how they want their password to be generated ex "C@@d%%%" where @ is only letters and where % is only numbers, and the user also inputs the numbers and letters to generate the password, then the program is suppose to print out something like cold123, but instead it prints out cood111 or clld111, I will post a snippet of my code below, but please don't bad mouth it, I'm fairly new to python, self-taught and just about couple of months into the python experience. 用户为程序提供了一个格式,说明他们希望如何生成密码“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) will replace every occurrence of the character at GenPass[Pos] with the value of number . GenPass.replace(GenPass[Pos],number)将取代的字符的每一个在发生GenPass[Pos]与值number You need to make sure you replace one character at a time. 您需要确保一次替换一个字符。

创建所有字符的列表和包含所有nums的列表,然后使用list.pop(randint(0,len(list) - 1)选择一个,你总是会选择一个不同的字母/数字,但你也会限制为10位数(0-9)和20位字母。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM