[英]Password generating that starts with uppercase and lower case in python
It should be uppercase and lowercase first. 首先应该是大写和小写。
Then it should be numbers and characters. 然后应该是数字和字符。
Then it should be a mixed of all characters. 然后,它应该是所有字符的混合。
import random
pswd = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbrs = "0123456789"
chars = "@#$&*_-:;',./^"
def pswd_generator(data):
if data == 1:
genpswd = "".join(random.sample(pswd,size))
elif data == 2:
genpswd ="".join(random.sample(pswd+numbrs+chars,size))
elif data == 3:
genpswd = "".join(random.sample(pswd+numbrs+chars,size))
print "Your Generated Password is:",genpswd
m = False
while not m:
size = int(raw_input("How many digits of password do you want to generate: "))
print "1.Weak Password"
print "2.Medium Password"
print "3.Strong Password"
print ("Select 1,2,3")
data = int(raw_input("Enter the option: "))
if data == 1:
m = True
elif data == 2:
m = True
elif data == 3:
m = True
else:
print "Wrong option.Try again"
pswd_generator(data)
How can I achieve in pythonic way? 如何以pythonic方式实现?
To clean it up and make it a bit more Pythonic along with meeting the goal of your password requirements I'd suggest the following: 为了进行清理,使其更加符合Python规范,同时满足密码要求的目标,我建议采取以下措施:
pswd_generator
function so that it accepts two arguments, an option
and size
. 更改您的pswd_generator
函数,使其接受两个参数: option
和size
。 The reason for this is that you're relying on the size
variable being created before the call to this function... pswd_generator
is only working because size
is in the global scope after the size =
这样做的原因是,你依靠size
变量调用此函数之前创建... pswd_generator
只工作,因为size
是在后全球范围内size =
pswd_generator
to ensure the option being passed in is valid. 在pswd_generator
执行一些非常基本的错误检查,以确保传递的选项有效。 def pswd_generator(option, size):
assert(option in (1, 2, 3))
size = max(6, size)
prefix_size = min(5, size / 3)
number_size = min(5, size / 3)
filler_size = size - prefix_size - number_size
if option == 1:
genpswd = "".join(random.sample(pswd, size))
elif option == 2:
genpswd = "".join(
random.sample(pswd, prefix_size) +
random.sample(numbrs + chars, number_size) +
random.sample(pswd + numbrs + chars, filler_size)
)
else:
genpswd = "".join(random.sample(pswd + numbrs + chars, size))
return genpswd
For your main loop I'd suggest: 对于您的主循环,我建议:
while
condition to simply look at whether a password has been created. 更改while
条件以仅查看是否已创建密码。 The way it's setup now you're using a variable to just break out of the loop...there are simpler ways to accomplish that. 现在,它的设置方式是使用变量来打破循环……有更简单的方法来实现。 psswd
variable which you can then print out and which will also terminate your loop. 如果是这样,请设置psswd
变量,然后可以将其打印输出,该变量也将终止循环。 psswd = None
while not psswd:
# stuff
option = int(raw_input("Enter the option: "))
if 0 < option < 4:
psswd = pswd_generator(option, size)
print "Your Generated Password is: ", psswd
else:
print "Wrong option.Try again"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.