我正在製作一個程序,運行時會根據我定義的字符列表生成一個隨機字符串。 的代碼是:Python字符串生成器,按照特定的順序
from random import randint
from random import *
import random
Upperletters=["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"]
Lowerletters = ["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"]
Symbols = ["!","$","%","^","&","*","(",")","_","-","+","="]
Numbers = ["1","2","3","4","5","6","7","8","9","0"]
random.shuffle(Symbols)
random.shuffle(Numbers)
random.shuffle(Upperletters)
random.shuffle(Lowerletters)
randomlength = randint(8, 12)
stringgenerator = ''.join(choice(Upperletters + Symbols + Numbers +
Lowerletters) for x in range(randomlength))
print(stringgenerator)
stringgeneratorlower = stringgenerator.lower()
letter_combos = ["qwe","wer","ert","rty","tyu","yui","uio","iop","asd","sdf","dfg","fgh","ghj","hjk","jkl","zxc","xcv","cvb","vbn","bnm"]
keyboard =int(sum(stringgeneratorlower.count(fragment) for fragment in letter_combos))
if keyboard >= 1:
print("Bad try again")
但我的問題是,我想在他們添加的順序使用符號串,所以它應該是大寫字符,然後再一些符號然後一些數字和然後是小寫字母。我的問題是,它會以完全隨機的順序生成它們。我怎樣才能讓它按照我想要的順序生成它們,以便生成的字符串不包含列表中的任何letter_combos?要做到這一點
你想它使用Upperletters,Lowerletters,符號和數字的順序,他們是在陣列中或做你想讓它先用Upperletters,然後用Lowerletters,然後用符號和最後使用數字? – kyle
您的導入是冗餘/不一致的。爲什麼不下定決心? –
您能舉出電流輸出和期望輸出的例子嗎? – Neo