-2

Why is the below program right? It checks if the list has 1,2,3 sequence present. Now, should the else statement be not in indentation with if line, rather than for line. As soon as I put it under if, other tests of this program go wrong. Please help explain.

def array123(nums):
  for x in range(len(nums)-2):
    if nums[x]==1 and nums[x+1]==2 and nums[x+2]==3:
      return True
  else:
    return False

array123([1, 1, 2, 3, 1])
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
SahiL
  • 13
  • 3
  • Please format your code using `control + k` and try to simplify your problem as much as you can. – csandreas1 May 26 '20 at 10:58
  • 1
    This is the [`for/else`](https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops) construct. The `else` will only be executed if the loop was exhausted without hitting the `break` statement inside the `if`. If you put the else inside the loop it will execute every time the condition is not met – Tomerikoo May 26 '20 at 10:59
  • 1
    Thank you! None of the books teach about this, just if/else construct. So as a basic programmer, I would always do else after my if & never understand whts the error. – SahiL May 26 '20 at 11:01
  • BTW, an alternative for this construct in your case can be using the [`any()`](https://docs.python.org/3/library/functions.html#any) function: `return any(nums[x:x+3] == [1, 2, 3] for x in range(len(nums)-2))` – Tomerikoo May 26 '20 at 11:03
  • Thank you, read the similar post as well, much clear of this useless option. Thank you guys – SahiL May 26 '20 at 11:14
  • Related (alternative solution): https://stackoverflow.com/questions/28802019/checking-if-a-list-contains-a-certain-sequence-of-numbers – Tomerikoo May 26 '20 at 11:17

1 Answers1

0

From here: https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

4.4. break and continue Statements, and else Clauses on Loops#

Loop statements may have an else clause; it is executed when the loop terminates through exhaustion of the iterable (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplified by the following loop, which searches for prime numbers:

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print(n, 'equals', x, '*', n//x)
...             break
...     else:
...         # loop fell through without finding a factor
...         print(n, 'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

(Yes, this is the correct code. Look closely: the else clause belongs to the for loop, not the if statement.)

When used with a loop, the else clause has more in common with the else clause of a try statement than it does with that of if statements: a try statement’s else clause runs when no exception occurs, and a loop’s else clause runs when no break occurs. For more on the try statement and exceptions, see Handling Exceptions.

NewPythonUser
  • 361
  • 1
  • 9
  • A query on this. I understood the whole other answers, but y is the 1st answer 2 is a prime number? coz on the 1st run, wont it be 2%2 which is 0.. . I know 2 is a prime number, but just asking about this program, – SahiL May 26 '20 at 11:19
  • The inner for-loop does not enter the loop when n == 2 because range(2,2) == [] – NewPythonUser May 26 '20 at 11:21
  • of course !!!!! Thanks ! – SahiL May 26 '20 at 12:01