如何减少变量数量并提高代码效率
how to reduce number of variables and make the code efficient
这是我第一次发布到 Whosebug
创建和使用此类变量的有效方法是什么
lowerCharsEnabled = True
upperCharsEnabled = True
digitCharsEnabled = True
specialCharsEnabled = True
这个函数可以完成这项工作,但据我所知,这不是最有效的方法
def usedChars():
global finalChars
if lowerCharsEnabled == True:
finalChars = lowerChars
if upperCharsEnabled == True:
finalChars = finalChars + upperChars
if digitCharsEnabled == True:
finalChars = finalChars + digitChars
if specialCharsEnabled == True:
finalChars = finalChars + specialChars
期待任何帮助的建议
完整程序代码供参考
import random
import string
lowerChars = string.ascii_lowercase
upperChars = string.ascii_uppercase
digitChars = string.digits
specialChars = string.punctuation
lowerCharsEnabled = True
upperCharsEnabled = True
digitCharsEnabled = True
specialCharsEnabled = True
finalChars = ""
passLen = 8
def usedChars():
global finalChars
if lowerCharsEnabled == True:
finalChars = lowerChars
if upperCharsEnabled == True:
finalChars = finalChars + upperChars
if digitCharsEnabled == True:
finalChars = finalChars + digitChars
if specialCharsEnabled == True:
finalChars = finalChars + specialChars
def generatePass():
try:
password = "".join(random.choice(finalChars) for x in range(passLen))
print(password)
except:
print("Cant Generate Pass")
usedChars()
generatePass()
input("Waiting...")
最终回答感谢大家
import string
import secrets
lower_chars = string.ascii_lowercase
upper_chars = string.ascii_uppercase
digit_chars = string.digits
special_chars = string.punctuation
lower_chars_enabled = True
upper_chars_enabled = True
digit_chars_enabled = True
special_chars_enabled = True
final_chars = ""
passLen = 8
def used_chars():
global final_chars
if lower_chars_enabled == True:
final_chars = lower_chars
if upper_chars_enabled == True:
final_chars = final_chars + upper_chars
if digit_chars_enabled == True:
final_chars = final_chars + digit_chars
if special_chars_enabled == True:
final_chars = final_chars + special_chars
def generate_pass():
password = "".join(secrets.choice(final_chars) for x in range(passLen))
return password
used_chars()
generate_pass()
input("Waiting...")
这是我第一次发布到 Whosebug
创建和使用此类变量的有效方法是什么
lowerCharsEnabled = True
upperCharsEnabled = True
digitCharsEnabled = True
specialCharsEnabled = True
这个函数可以完成这项工作,但据我所知,这不是最有效的方法
def usedChars():
global finalChars
if lowerCharsEnabled == True:
finalChars = lowerChars
if upperCharsEnabled == True:
finalChars = finalChars + upperChars
if digitCharsEnabled == True:
finalChars = finalChars + digitChars
if specialCharsEnabled == True:
finalChars = finalChars + specialChars
期待任何帮助的建议
完整程序代码供参考
import random
import string
lowerChars = string.ascii_lowercase
upperChars = string.ascii_uppercase
digitChars = string.digits
specialChars = string.punctuation
lowerCharsEnabled = True
upperCharsEnabled = True
digitCharsEnabled = True
specialCharsEnabled = True
finalChars = ""
passLen = 8
def usedChars():
global finalChars
if lowerCharsEnabled == True:
finalChars = lowerChars
if upperCharsEnabled == True:
finalChars = finalChars + upperChars
if digitCharsEnabled == True:
finalChars = finalChars + digitChars
if specialCharsEnabled == True:
finalChars = finalChars + specialChars
def generatePass():
try:
password = "".join(random.choice(finalChars) for x in range(passLen))
print(password)
except:
print("Cant Generate Pass")
usedChars()
generatePass()
input("Waiting...")
最终回答感谢大家
import string
import secrets
lower_chars = string.ascii_lowercase
upper_chars = string.ascii_uppercase
digit_chars = string.digits
special_chars = string.punctuation
lower_chars_enabled = True
upper_chars_enabled = True
digit_chars_enabled = True
special_chars_enabled = True
final_chars = ""
passLen = 8
def used_chars():
global final_chars
if lower_chars_enabled == True:
final_chars = lower_chars
if upper_chars_enabled == True:
final_chars = final_chars + upper_chars
if digit_chars_enabled == True:
final_chars = final_chars + digit_chars
if special_chars_enabled == True:
final_chars = final_chars + special_chars
def generate_pass():
password = "".join(secrets.choice(final_chars) for x in range(passLen))
return password
used_chars()
generate_pass()
input("Waiting...")