我的if子句检查chars-variable是“字母”还是“字母”(“数字”或“数字”)并不像我想要的那样... 即使没有调用子句,它也会更改chars-variable。 我已经尝试了一些东西,但似乎没有任何工作。 (添加了测试打印件)
(我不想对这个程序做任何违法的事情。我只是想了解一下itertools-module。)
代码:
import itertools, sys
chars = input("Chars:" + " ")
max_length = input("Max length:" + " ")
password = input("Password:" + " ")
max_length = int(max_length)
print() #
print("Chars test:", chars) #
if chars == "letters" or "Letters":
chars = "abcdefghijklmnopqrstuvwxyz"
print("Chars test:", chars) #
print("Chars test:", chars) #
if chars == "numbers" or "Numbers":
chars = "0123456789"
print("Chars test:", chars) #
print("Chars test:", chars) #
print()
input("Press enter to start...")
print()
length = (max_length + 1) - max_length
while length <= max_length:
results = itertools.product(chars, repeat = length)
for result in results:
result = "".join(result)
if result != password:
print("Password not found yet:", result)
else:
print()
print("Password found:", result)
print()
input("Press enter to exit...")
sys.exit()
length = length + 1
print()
print("Couldn't find the password.")
# = Remove when solved.
答案 0 :(得分:2)
if chars == "letters" or "Letters":
没有做你认为的事情。它是True
,如果
chars == "letters"
或
"Letters"
由于Python中的"Letters"
始终为True
,因此if
子句始终为True
。
这样做:
if chars in ("letters", "Letters"):
和
if chars in ("numbers", "Numbers"):