Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functions - Prime Check in Python3 Returns Multiple Values #43

Open
paper-skyline opened this issue Oct 26, 2018 · 1 comment
Open

Functions - Prime Check in Python3 Returns Multiple Values #43

paper-skyline opened this issue Oct 26, 2018 · 1 comment

Comments

@paper-skyline
Copy link

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
@abhinav4848
Copy link

abhinav4848 commented Mar 23, 2019

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants