-2

My program seems to be looping several times here despite a break:

for sequence in qwerty:
    for char in range(0,passlen):
        if password [char : char + 2] in sequence:
           score -= 5
           print(score,"subtracting 5")

It usually outputs this:

38 subtracting 5
33 subtracting 5
28 subtracting 5
23 subtracting 5
18 subtracting 5
13 subtracting 5
8 subtracting 5
3 subtracting 5
-2 subtracting 5
-7 subtracting 5

Or nothing at all.

It should only loop equal to the number of 3 letter QWERTY sequences in password.

What should I do to mediate this?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
Jake.h
  • 1
  • 2

2 Answers2

0

The break only breaks the inner-most loop (i.e. for char in range(0,passlen):) so you go back to the outer loop and move on.

You can use a flag instead:

flag = False
for sequence in qwerty:
          if flag: # False until the inner loop breaks
               break
          for char in range(0,passlen):
            if password [char : char + 2] in sequence:
               score -= 5
               print(score,"subtracting 5")
               flag = True
               break
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
0

the outer for loops seems redundant in this program. The continued looping you refer is the "for sequence in qwerty:", you should either 1) delete the outer for loop or 2) add a break statement for the outer loop

Nicholas
  • 21
  • 3