想要使字母,数字和允许的符号(“ ^”,“ *”,“ $”,“!”,“%”,“&”,“(”,“)”,“ -“,” _“,” =“,” +“)长10个字符(据说是一个完美的密码),但是不能这样做!这是到目前为止我得到的:
import random
password="##########"
#Pick Length
pwordLen = random.randint(8,24)
lowerConsonants = ("b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z")
upperConsonants = ("B","C","D","F","G","H","J","K","L","M","N","P","Q","R","S","T","V","W","X","Y","Z")
lowerVowels = ("a","e","u","o","i")
upperVowels=("A","E","U","O","I")
numbers = ("0","1","2","3","4","5","6","7","8","9")
symbols = ("^","*","$","!","%","&","(",")","-","_","=","+")
#For character 1 of 24
selectType1=random.randint(1,6)
if selectType1==1 or selectType1==2:
selectIndividual1=random.randint(1,26)
selectIndividual1=selectIndividual1-1
elif selectType1==3 or selectType1==4:
selectIndividual1=random.randint(1,5)
selectIndividual1=selectIndividual1-1
elif selectType1==5:
selectIndividual1=random.randint(1,10)
selectIndividual1=selectIndividual1-1
else:
selectIndividual1=random.randint(1,12)
selectIndividual1=selectIndividual1-1
if selectType1==1:
password[0]=lowerConsonants[selectIndividual1]
elif selectType1==2 :
password[0]=upperConsonants[selectIndividual1]
elif selectType1==3 :
password[0]=lowerVowels[selectIndividual1]
elif selectType1==4 :
password[0]=upperVowels[selectIndividual1]
elif selectType1==5 :
password[0]=numbers[selectIndividual1]
else:
password[0]=symbols[selectIndividual1]
print(password)
然后我将每个数字的所有'selectIndividual1'和'selectType1'替换为10,然后将这10个字符合并成字符串。
此刻我收到此错误:
Traceback (most recent call last):
File "/Users/_____/Documents/temp5.py", line 34, in <module>
password[0]=lowerVowels[selectIndividual1]
TypeError: 'str' object does not support item assignment
我在Mac上运行Python 3.7.0
答案 0 :(得分:2)
带有随机和字符串库:
import string
import random
all_char = string.ascii_letters + string.punctuation + string.digits
password = "".join(random.choice(all_char) for x in range(random.randint(10)))
//Y+QV9dXGSh
//G?|Q5i?0\\c
编辑
字符串集是:
string.letters:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
string.punctuation:
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
string.digits:
0123456789
答案 1 :(得分:1)
该解决方案如何?
import string
import random
pwd = []
for i in range(10):
r = random.choice(string.letters + string.punctuation + string.digits)
pwd.append(r)
print ''.join(pwd)
输出:
xS1]O3_z~I
打印string.letters
,string.punctuation
和string.digits
:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
0123456789
答案 2 :(得分:1)
问题是,如上所述,在python中,不允许编辑str
对象。它们是“不变的”。解决此问题的方法是只添加您的单个字符:pass = pass + selected_char
(这不会更改pass对象,而是会创建一个全新的字符)。
例如
password = ""
# your char picking logic here
if selectType == 1:
password += lowerConsonants[selectIndividual]
# the rest of your code
答案 3 :(得分:0)
使用random.choice
>>> import random
>>> all_chars = lowerConsonants + upperConsonants + lowerVowels + upperVowels + numbers+ symbols
>>> ''.join([random.choice(all_chars) for i in range(10)])
'W+8yB533$W'
答案 4 :(得分:0)
您正在尝试将数组元素保存到String中,因此会出现错误。
更改此:
Array[(With[{x = "member" <> ToString[#]},
Clear[x];
Evaluate[Symbol[x]] = list[[#]]]) &, Length[list]]
对此:
password="##########"
#Pick Length
pwordLen = random.randint(8,24)