The for loop at line 12 and the one nested within it, I mean. I've encountered situations like this more than once. I'd use a list comprehension but it doesn't seem like it would work here.
import random
import string
def password_generator():
key = zip(string.digits, string.ascii_uppercase)
cruft, x = str(random.random()).split('.')
pw = ''
for item in x:
for element in key:
if item in element:
Q = random.random()
if Q > 0.7:
pw += element[1].lower()
else:
pw += element[1]
print pw
Thanks.
Here's a way to use a list comprehension:
def pw_gen():
key = zip(string.digits, string.ascii_uppercase)
cruft, x = str(random.random()).split('.')
def f(i,e):
Q = random.random()
if Q > 0.7:
return e[1].lower()
else:
return e[1]
return [ f(item,element) for item in x for element in key if item in element ]
This returns a list of characters. Use "".join( pw_gen() )
to convert to a string.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加