Skip to content

Functions - Prime Check in Python3 Returns Multiple Values #43

Description

@paper-skyline

In Python3.7, entering this function and passing the number 5 to the function results in multiple return values of "True". What's causing that?

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

Activity

  1. abhinav4848 commented on Mar 23, 2019

    @abhinav4848

    this can only happen if while originally testing your function looked like

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

    instead of

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

    Note the indentation of the else clause.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions