【发布时间】:2022-02-10 06:48:08
【问题描述】:
我正在制作一个非常基本的密码生成器,它会询问用户他们想要的密码中的某些内容(例如,用户可以选择是否需要标点符号)。我已经完成了这个程序,它运行得非常好,但它包含了一部分 if 和语句:
if p == "N" and ucl == "N" and lcl == "N":
print("Invalid requirements ")
if p == "N" and ucl == "N" and lcl == "Y":
x = lclList
generate(x)
if p == "N" and ucl == "Y" and lcl == "N":
x = uclList
generate(x)
if p == "N" and ucl == "Y" and lcl == "Y":
x = uclList + lclList
generate(x)
if p == "Y" and ucl == "Y" and lcl == "Y":
x = pList + uclList + lclList
generate(x)
if p == "Y" and ucl == "N" and lcl == "Y":
x = pList + lclList
generate(x)
if p == "Y" and ucl == "Y" and lcl == "N":
x = pList + uclList
generate(x)
if p == "Y" and ucl == "N" and lcl == "N":
x = pList
generate(x)
正如我所说,它运行良好,但看起来非常混乱且效率低下 - 我已经可以想象如果我使用 4 个要求会变得多么令人费解,而且上帝禁止了。是否有另一种编程方式可以使其可重复且更高效?
旁注,这几乎是我编写的第一个功能齐全的程序,所以如果我违反了日内瓦的一些编程惯例,请不要感到震惊。
【问题讨论】:
-
您可以使用字典,它在 Python 中用作 switch case。
标签: python if-statement optimization passwords