-2
def prime_check(num):
    """
    Function to check if a number is prime
    """
    for n in range(2,num):
        if num%n == 0:
            print(num, "is not prime")
            break
        else:
            print(num, "is not prime")

pardon me as i just started using stack over flow and i don't know how to show the syntax highlighting here. but looking at the code above, when i call the function and i pass in the integer (number 2) into the function, there is no result, also when i pass in the number 21, it says 21 is prime, which is wrong. however, when using the code from udemy, which is a bit similar to mine(except for indentation), the function works fine. i need help understanding the indentation difference and why it works. The udemy code is shown below.

def is_prime(num):
    '''
    Naive method of checking for primes. 
    '''
    for n in range(2,num):
        if num % n == 0:
            print(num,'is not prime')
            break
    else: # If never mod zero, then prime
        print(num,'is prime!')

notice the difference in indentation between my own code and theirs. Please i need to understand the difference. Help!!!!!!!!!!

Harshal Parekh
  • 5,918
  • 4
  • 21
  • 43
Uche
  • 13
  • 5
  • 1
    In your code, the `else` belongs to the `if`, in their code it belongs to the `for`, as indicated by the indentation. Any Python tutorial should explain the role of indentation for forming blocks. – Robert Jun 09 '20 at 03:40
  • 1
    *i don't know how to show the syntax highlighting here* - please go through the tour and help pages - the least you could do. – Harshal Parekh Jun 09 '20 at 03:41
  • 2
    [for-else](https://book.pythontips.com/en/latest/for_-_else.html) in Python3. – leopardxpreload Jun 09 '20 at 03:41
  • Also, the way your code is indented, if `num `is `2` then it will never enter the for loop, which is why there is no result. – Sam Jun 09 '20 at 03:43
  • There is no case in which your code could say that a number is prime. Both of the `print` calls say "is not prime". – user2357112 Jun 09 '20 at 03:46
  • Thanks guys, the for-else documentation has helped a lot – Uche Jun 09 '20 at 09:46

2 Answers2

0

else occurs if the for loop did not break out. See Documenation.

leopardxpreload
  • 767
  • 5
  • 17
  • Looking at the first block of code I posted, when I pass in 2, there’s no answer. When I pass in 21, it says 21 is prime, which is wrong. These anomalies don’t happen when I pass in the same arguments in the second block of code. I really don’t know why – Uche Jun 09 '20 at 03:51
  • @Uche - Choose a prime number (7) - in the first block: it will fail the `if` statement and execute the `else` statement on each cycle. In the second block: it will never satisfy the `if` statement and then after the `for loop` the `else` is called. NOTE: The `else` will never be called if the `break` occurs. – leopardxpreload Jun 09 '20 at 03:58
-2

In the above program else statement is executed when the condition for the for loop when the range limit is execeeded that is fails.. i.e) for loop checks whether the num is divisible by n if it is divisible it returns "Not a Prime Number" ...After the for loop condition ends else part will be executed.

Srikeshram
  • 87
  • 1
  • 5