我想检查通过函数中的参数给出的字符串是否在列表中。代码本身不会产生任何错误,但它的工作方式错误。如果我给我的功能" -a"作为参数,它仍然表示它不在列表中,但肯定是。
这是代码:
def generatePassword(pLength, mode):
password = str()
commands = ["-a", "-n", "-s", "-allupper", "-mixupper"]
alphabet = ["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"]
specialCharacters = ["!", "?", "&", "@",
"-", "=", "#", "+", "*", "/", "%", "§"]
if mode.lower().split() not in commands:
print("Couldn't recognize commands...")
else:
for n in range(pLength):
i = random.randint(1, 2)
if "-a" in mode.lower().split():
password += alphabet[random.randint(0, 25)]
print("[+]", password)
generatePassword(30, "-a")
答案 0 :(得分:2)
你的病情不好:
if mode.lower().split() not in commands:
print("Couldn't recognize commands...")
将其替换为(例如):
args = set(mode.lower().split())
if not set(args).issubset(set(commands)):
print("Couldn't recognize commands...")
http://python.6.x6.nabble.com/Test-if-list-contains-another-list-td1506964.html
答案 1 :(得分:0)
您可以使用any
命令检查mode.lower().split()
中的任何单词是否不在命令中:
def generatePassword(pLength, mode):
password = str()
commands = ["-a", "-n", "-s", "-allupper", "-mixupper"]
alphabet = ["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"]
specialCharacters = ["!", "?", "&", "@",
"-", "=", "#", "+", "*", "/", "%", "§"]
if any(x not in commands for x in mode.lower().split()):
print("Couldn't recognize commands...")
else:
for n in range(pLength):
i = random.randint(1, 2)
if "-a" in mode.lower().split():
password += alphabet[random.randint(0, 25)]
print("[+]", password)
generatePassword(30, "-a")
答案 2 :(得分:-1)
更改
mode.lower().split()
到
mode.lower()