jeudi 23 mars 2017

How to generate random choices for random.choice() function using dictionary

I am trying to create a function which generates a random password using random.choice function,but i also want the choices to be random,i am trying to use a dict for this purpose,but not getting the desired result.. My code is:

def makepassword():
    letter1=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    letter2=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
    symbol1=["@","#","$","%","&","*","<",">"]
    symbol2=["@","#","$","%","&","*","<",">"]
    number1=["0","1","2","3","4","5","6","7","8","9"]
    dict1={0:"letter1",1:"letter2",2:"symbol1",3:"symbol2",4:"number1"}
    k=dict1.get(randint(0,4))
    l=dict1.get(randint(0,4))
    m=dict1.get(randint(0,4))
    print(k,l,m)
    password=(choice(k)+choice(l)+choice(m))
    print(password)

If i print and check the values of k,l,m am getting values such as letter1/letter2/symbol1/symbol2/number1 so trying to use those values in choice function.

where as when i directly use: password=(choice(letter1)+choice(symbol1)+choice(number1)) it works fine.

So how the above Choice function can be implemented using Dictionary.




Aucun commentaire:

Enregistrer un commentaire