0

I need your help. I got into learning python and having learnt some theory, I decided to try my hands on something simple(a code which compares its result to the value to a given variable). I have a variable 'pin' and assigned a list of numbers to it. Then I created another variable 'numArray' and also assigned a list to it. Another variable 'access' with a empty list assigned. Now, I have nested 'for loops' which iterate through the 'numArray' and the result is reassigned to the 'access' variable and then 'access' gets printed out every time. Next, I compare 'access' with 'pin' and if both are the same, then a statement is printed and the loop should be exited. Everything seems to work fine up to this point. Instead of the loop ending at {0,4,5,6], it gets there, prints the statement, skips - [0,4,5,7],[0,4,5,8],[[0,4,5,9] - and continues the loop from [0,4,6,0].

I know I am doing something wrong but I cannot figure out what it is. I would be glad if anyone can help me find what the issue is or even how to write this in a more efficient way as I plan to expand on this as my knowledge progresses. By the way, I am just practicing how to use conditionals and loops. Thanks in advance.

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
khelwood
  • 55,782
  • 14
  • 81
  • 108
  • You `break` is inside five loops. Only the innermost one, `for p4 in numArray`, is exited by the break statement. – khelwood Jul 19 '20 at 13:58
  • 1
    Does this answer your question? [How to break out of multiple loops?](https://stackoverflow.com/questions/189645/how-to-break-out-of-multiple-loops) – mkrieger1 Jul 19 '20 at 14:14
  • @khelwood Even after placing the `break` outside the five loops doesn't stop the loops from running when it matches the pin or do you mean I should have a break for each loop? – Michael Olasanya Jul 19 '20 at 17:00
  • @mkrieger1 Yeah.. Some complex examples though but I'll take some time to study the answers. Thanks – Michael Olasanya Jul 19 '20 at 17:09

1 Answers1

0

You can use another variable to break all loops, here I am using stop If the pin is matched then you can break all loops. please see my code

or You can put your code in a function and when pin matched then return the function.

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

You can optimize these type of code using dictionary if the order of elements does not matter.

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")
saurabh sisodia
  • 312
  • 1
  • 8
  • Thanks for the help. I never thought about using another variable like "stop" with a boolean assigned to it to break out of the loops. Now I have an added knowledge..haha.... I didn't really understand the second code using the module "collections". I will try to study the code and read more about that module. – Michael Olasanya Jul 19 '20 at 16:45
  • Happy to help you @Michael Olasanya, If it solves your problem then please Accept this answer – saurabh sisodia Jul 19 '20 at 18:19