Python3 遍历列表的 For 循环不会中断
Python3 For loop iterating through a list won't break
跳出 for 循环
我的代码不会跳出循环:
(我删掉了一些装饰品部分)
lchars = ['', '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', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '\', '#', '$', '%', '^', '&', '*', '(', ')', '-', '=', '_', '+', '`', '~', '[', ']', '{', '}', '|', ';', ':', "'", ',', '.', '/', '<', '>', '?', '`', '¡', '™', '£', '¢', '∞', '§', '¶', '•', 'ª', 'º', '–', '≠', 'œ', '∑','´', '®', '†', '¥', '¨', 'ˆ', 'ø', 'π', '“', '‘', '«', 'å', 'ß', '∆', '˚', '¬', '…', '˜', 'µ', '≤', '≥', '÷', 'æ', 'Ω', '≈', 'ç', '√', '"', ' ']
guessThisPass = str(input("Enter the password you want the python-based brute force hacker to guess (Character limit is 4): "))
if len(guessThisPass) > 4:
print('I SAID CHARACTER LIMIT IS 4!! I AM TRUNCATING YOUR PASSWORD NOW! ')
guessThisPass[0:4]
time.sleep(5)
print("Starting...")
time.sleep(2)
start = time.time()
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
tryPass = str(str(a) + str(b) + str(c) + str(d)). # I know the strs are probably unnessecary.
print(tryPass). # Outputing the attempted password
if tryPass == guessThisPass:
break # This never happens
print("Whoo!")
print("That took ", time.time()-start, "seconds.")
使用此代码,假设我的密码是 'a'(哪个 IRL,它是 而不是 )。那么,按道理,应该几乎是瞬间就可以突破了吧?除了它没有;它只是继续前进,不会中断,甚至会达到“^Bc”或更高的组合。为什么它不会破裂?我是否需要在每个循环中添加 if 语句?
测试人员代码
此外,这是我用来测试可能组合的代码:
(也截去多余的化妆品)
# All possible combinations
lchars = ['', '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', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '\', '#', '$', '%', '^', '&', '*', '(', ')', '-', '=', '_', '+', '`', '~', '[', ']', '{', '}', '|', ';', ':', "'", ',', '.', '/', '<', '>', '?', '`', '¡', '™', '£', '¢', '∞', '§', '¶', '•', 'ª', 'º', '–', '≠', 'œ', '∑','´', '®', '†', '¥', '¨', 'ˆ', 'ø', 'π', '“', '‘', '«', 'å', 'ß', '∆', '˚', '¬', '…', '˜', 'µ', '≤', '≥', '÷', 'æ', 'Ω', '≈', 'ç', '√', '"', ' ']
# print("Also, there are ", len(lchars), "characters in our character database\n\n")
from time import time
start = time()
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
print(a+b+c+d)
print("Whoo!")
print("That took ", time()-start, "seconds.")
我检查了上面代码的输出。字母 'a' 在输出中。它应该 有效。为什么它不起作用?
是的,break
只会让你跳出最内层的循环!我会将该代码移动到一个函数中,它可以 return tryPass
找到正确的函数。
编码愉快!
它正在 break
ing。问题是,break
仅从内部循环中断;不是每个包含它的循环。最内层的循环中断,然后围绕它的循环从它停止的地方开始。
在不过多更改代码的情况下,最简单的解决方案是将该代码包装在一个函数中,然后 return
从中提取:
def try_all(lchars):
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
tryPass = a + b + c + d
print(tryPass)
if tryPass == guessThisPass:
return tryPass
cracked_password = try_all(lchars)
还有许多其他的改进应该提到,但在这里不合适。代码运行并完成后,您可以 post 在 Code Review 上获取一般建议。
实际上它确实中断了,但是由于您有 4 个嵌套循环,它只是中断了最内层的循环。你可以做几件事来解决这个问题:
- 创建一个标志并在继续之前检查它
- 将您的代码移动到一个函数中,然后 return 当您找到答案时
- 使用
itertools.product
创建您的试用字符串。这样你只需要一个循环,所以 breaking 会很简单。
跳出 for 循环
我的代码不会跳出循环:
(我删掉了一些装饰品部分)
lchars = ['', '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', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '\', '#', '$', '%', '^', '&', '*', '(', ')', '-', '=', '_', '+', '`', '~', '[', ']', '{', '}', '|', ';', ':', "'", ',', '.', '/', '<', '>', '?', '`', '¡', '™', '£', '¢', '∞', '§', '¶', '•', 'ª', 'º', '–', '≠', 'œ', '∑','´', '®', '†', '¥', '¨', 'ˆ', 'ø', 'π', '“', '‘', '«', 'å', 'ß', '∆', '˚', '¬', '…', '˜', 'µ', '≤', '≥', '÷', 'æ', 'Ω', '≈', 'ç', '√', '"', ' ']
guessThisPass = str(input("Enter the password you want the python-based brute force hacker to guess (Character limit is 4): "))
if len(guessThisPass) > 4:
print('I SAID CHARACTER LIMIT IS 4!! I AM TRUNCATING YOUR PASSWORD NOW! ')
guessThisPass[0:4]
time.sleep(5)
print("Starting...")
time.sleep(2)
start = time.time()
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
tryPass = str(str(a) + str(b) + str(c) + str(d)). # I know the strs are probably unnessecary.
print(tryPass). # Outputing the attempted password
if tryPass == guessThisPass:
break # This never happens
print("Whoo!")
print("That took ", time.time()-start, "seconds.")
使用此代码,假设我的密码是 'a'(哪个 IRL,它是 而不是 )。那么,按道理,应该几乎是瞬间就可以突破了吧?除了它没有;它只是继续前进,不会中断,甚至会达到“^Bc”或更高的组合。为什么它不会破裂?我是否需要在每个循环中添加 if 语句?
测试人员代码
此外,这是我用来测试可能组合的代码:
(也截去多余的化妆品)
# All possible combinations
lchars = ['', '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', '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', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '!', '@', '\', '#', '$', '%', '^', '&', '*', '(', ')', '-', '=', '_', '+', '`', '~', '[', ']', '{', '}', '|', ';', ':', "'", ',', '.', '/', '<', '>', '?', '`', '¡', '™', '£', '¢', '∞', '§', '¶', '•', 'ª', 'º', '–', '≠', 'œ', '∑','´', '®', '†', '¥', '¨', 'ˆ', 'ø', 'π', '“', '‘', '«', 'å', 'ß', '∆', '˚', '¬', '…', '˜', 'µ', '≤', '≥', '÷', 'æ', 'Ω', '≈', 'ç', '√', '"', ' ']
# print("Also, there are ", len(lchars), "characters in our character database\n\n")
from time import time
start = time()
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
print(a+b+c+d)
print("Whoo!")
print("That took ", time()-start, "seconds.")
我检查了上面代码的输出。字母 'a' 在输出中。它应该 有效。为什么它不起作用?
是的,break
只会让你跳出最内层的循环!我会将该代码移动到一个函数中,它可以 return tryPass
找到正确的函数。
编码愉快!
它正在 break
ing。问题是,break
仅从内部循环中断;不是每个包含它的循环。最内层的循环中断,然后围绕它的循环从它停止的地方开始。
在不过多更改代码的情况下,最简单的解决方案是将该代码包装在一个函数中,然后 return
从中提取:
def try_all(lchars):
for d in lchars:
for c in lchars:
for b in lchars:
for a in lchars:
tryPass = a + b + c + d
print(tryPass)
if tryPass == guessThisPass:
return tryPass
cracked_password = try_all(lchars)
还有许多其他的改进应该提到,但在这里不合适。代码运行并完成后,您可以 post 在 Code Review 上获取一般建议。
实际上它确实中断了,但是由于您有 4 个嵌套循环,它只是中断了最内层的循环。你可以做几件事来解决这个问题:
- 创建一个标志并在继续之前检查它
- 将您的代码移动到一个函数中,然后 return 当您找到答案时
- 使用
itertools.product
创建您的试用字符串。这样你只需要一个循环,所以 breaking 会很简单。