Python:如何“刷新”随机变量

时间:2016-10-17 12:00:11

标签: python list variables random

我有这个代码,我想知道无论如何我可以摆脱所有这些变量,而只是为每个目的只有一个。我的代码是密码生成器,具有三种不同的密码强度。代码将在没有这个的情况下工作,但出于设计考虑,我希望将其缩小。我试过让它们起作用但是没用。我可能错过了一些明显的东西:

import time
import random
import string

ps = ""
name = ""
ans1 = ""
list_s = ["!","£","~","%","*","(","}","#",";","@","/",":","-"]
list_w = ["mouse","human","dog","tree","boom","thief","killer","dealer","feeler","man","woman","death"]
list_w2 =["help","yelp","empire","squire","lier","fryer","kitchen","bed","matress", "criminal","drug"]

rc1 = random.choice(string.ascii_lowercase)
rc2 = random.choice(string.ascii_lowercase)
rc3 = random.choice(string.ascii_lowercase)
rc4 = random.choice(string.ascii_lowercase)
rc5 = random.choice(string.ascii_uppercase)
rc6 = random.choice(string.ascii_uppercase)
rc7 = random.randrange(10)
rc8 = random.randrange(10)
rc9 = random.randrange(10)
rc10 = random.choice(list_s)
rc11 = random.choice(list_s)
rc12 = random.choice(list_w)
rc13 = random.choice(list_w2)

while name == "":
    name = str(input("Welcome to password64, before we begin enter your name:"))
while ans1 == "":
    ans1 = str(input("Have you used password64 before " + name + " ?"))

ans1 = ans1[0].lower()

if ans1 == "n":
    print ("Thank you for trying  password64 for the first time " + name + " !")
    time.sleep(4)
    print ("I will ask you the strength of the password you want, there will be three options: Strong, Medium or Weak")
    time.sleep(6)
    print ("A strong password is a mix of random numbers, letters and symbols")
    time.sleep(3)
print ("A medium password will be a couple of words with random numbers and symbols")
    time.sleep(3)
    print ("A weak password will be a couple of words with a number")
elif ans1 == "y":
    print ("Thank you for reusing password64 " + name + " !")
    time.sleep(2)
    print ("Let's get straight on with it then")


while ps == "":
    ps = str(input("Do you with to generate a strong, medium or weak password?"))
    ps = ps[0].lower()

if ps == "s":
    a = [rc1 , rc2, rc3, rc4, rc5, rc6, rc7, rc8, rc9, rc10, rc11]
elif ps == "m":
    a = [rc12, rc13, rc7, rc8, rc10, rc11]
elif ps == "w":
    a = [rc12, rc13, rc7]
else:
    print ("You did not enter a valid password strength")
    exit()

random.shuffle(a,random.random)

print ("Your password is generating....")

for i in range(0,len(a)):
    time.sleep(1)
    print (a[i], end="")
print ("")
print ("Your password is ready to be used " + name + ". Thank you for using    password64")

1 个答案:

答案 0 :(得分:0)

为什么不把这些随机数放入函数中?

f.e。

# -*- coding: cp1252 -*-
import string
import random
import time

random_type = ("lower","upper","digit","strange","wordlist1","wordlist2")

list_s = ["!","£","~","%","*","(","}","#",";","@","/",":","-"]
list_w = ["mouse","human","dog","tree","boom","thief","killer","dealer","feeler","man","woman","death"]
list_w2 =["help","yelp","empire","squire","lier","fryer","kitchen","bed","matress", "criminal","drug"]

def random_part(which_type):
    if which_type == "lower":
        return random.choice(string.ascii_lowercase)
    elif which_type == "upper":
        return random.choice(string.ascii_uppercase)
    elif which_type == "digit":
        return str(random.randrange(10))
    elif which_type == "digit":
        return str(random.randrange(10))
    elif which_type == "strange":
        return random.choice(list_s)
    elif which_type == "wordlist1":
        return random.choice(list_w)
    else:
        return random.choice(list_w2)

def generate_pw(password_strength):
    if password_strength == 'w':
        l = ['wordlist1','wordlist2','digit']
    elif password_strength == 'm':
        l = ['wordlist1','wordlist2','digit','digit','strange','strange']
    else:
        l = ['wordlist1','wordlist2','digit','digit','strange','strange','lower','upper']  #whatever you want to

    result = []
    for x in l:
        result.append(random_part(x))
    return result

print generate_pw('s')
顺便说一下:如果要从列表中删除选择的值,则只能使用一个单词列表。因此,您可以在密码中使用2个以上的单词。与奇怪的字符相同,所以每个字符只能出现一次。 并且jaou可以洗牌结果。 但这是你的决定