【问题标题】:I cannot successfully exit the for loops in my python code, Why?我无法成功退出 python 代码中的 for 循环,为什么?
【发布时间】:2020-11-08 20:46:35
【问题描述】:

我需要你的帮助。我开始学习 python 并学习了一些理论,我决定尝试一些简单的东西(将其结果与给定变量的值进行比较的代码)。我有一个变量“pin”并为其分配了一个数字列表。然后我创建了另一个变量“numArray”并为其分配了一个列表。分配了一个空列表的另一个变量“访问”。 现在,我嵌套了“for循环”,它遍历“numArray”,结果被重新分配给“access”变量,然后每次都打印出“access”。 接下来,我比较 'access' 和 'pin',如果两者相同,则打印一条语句并退出循环。到目前为止,一切似乎都很好。而不是在 {0,4,5,6] 结束的循环,它到达那里,打印语句,跳过 - [0,4,5,7],[0,4,5,8],[[0, 4,5,9] - 并从 [0,4,6,0] 继续循环。

我知道我做错了什么,但我不知道是什么。如果有人可以帮助我找到问题所在,或者甚至如何以更有效的方式编写此内容,我将很高兴,因为我计划随着知识的进步对此进行扩展。顺便说一句,我只是在练习如何使用条件和循环。提前致谢。

import time

pin = [0,4,5,6]
numArray = [0,1,2,3,4,5,6,7,8,9]
found = True
access = []

while found:
    for p1 in numArray:
        for p2 in numArray:
            for p3 in numArray:
                for p4 in numArray:
                    access = [p1,p2,p3,p4]
                    time.sleep(0.01)
                    print(access)
                    if access == pin:
                        print('Access Granted - The pin is:', access)
                        found = False
                        break
                    else:
                        continue

【问题讨论】:

  • break 在五个循环中。只有最里面的 for p4 in numArray 会被 break 语句退出。
  • 这能回答你的问题吗? How to break out of multiple loops?
  • @mkrieger1 是的。虽然有一些复杂的例子,但我会花一些时间来研究答案。谢谢

标签: python python-3.x for-loop if-statement nested-loops


【解决方案1】:

你可以使用另一个变量来打破所有循环,这里我使用stop 如果引脚匹配,那么您可以断开所有循环。请看我的代码

或者您可以将您的代码放在function 中,当引脚匹配时,然后return 函数。

import time

pin = [0,4,5,6]
numArray = [0,1,2,3,4,5,6,7,8,9]
found = True
access = []
stop=False       # initialize stop to False
while found:
    for p1 in numArray:
        for p2 in numArray:
            for p3 in numArray:
                for p4 in numArray:
                    access = [p1,p2,p3,p4]
                    time.sleep(0.01)
                    print(access)
                    if access == pin:
                        print('Access Granted - The pin is:', access)
                        found = False
                        stop=True     # if pin is matched then set stop=True
                        break
                    else:
                        continue
                if stop:     
                    break
            if stop:
                break
        if stop:
            break

如果元素的顺序无关紧要,您可以使用dictionary 优化这些类型的代码。

from collections import defaultdict   # use dictionary with default value int

d=defaultdict(int)   # d is our dictionary 
                     
pin = [0,4,5,6]
numArray = [0,1,2,3,4,5,6,7,8,9]
for v in numArray:
    d[v]+=1         # key will be elements of numArray and value is their count in numArray
ans=True           # set default ans  True
for v in pin:      # iterate through pin list
    if d[v]==0:    # if element does not exists in d then ans will be False  and break the for loop
        ans=False
        break 
    d[v]-=1        # else decrement count of current element by one, for not to use same element again
if ans:              
    print("Acces granted")
else:
    print("pin not found")

【讨论】:

  • 感谢您的帮助。我从没想过使用另一个变量,如“stop”,并为其分配一个布尔值来打破循环。现在我有了额外的知识..哈哈....我并没有真正理解使用模块“集合”的第二个代码。我将尝试研究代码并阅读有关该模块的更多信息。
  • 很高兴帮助你@Michael Olasanya,如果它解决了你的问题,那么请接受这个答案
猜你喜欢
  • 1970-01-01
  • 2016-02-11
  • 1970-01-01
  • 2021-09-01
  • 1970-01-01
  • 2022-06-14
  • 2022-12-04
  • 2019-08-29
  • 1970-01-01
相关资源
最近更新 更多