vitalious
vitalious

Reputation: 311

prime number function not printing output

I made a function that checks if the number is a prime, but when I call it, I don't get any screen output:

def is_prime(x):
    x = int(x)
    if x < 0 or x == 1:
        return False
        print('no')
    else:
        temp_div = 10
        while temp_div > 1:
            if x % temp_div == 0:
                return False
                print('no')
                break
            else:
                temp_div -= 1
        if temp_div == 1:
            return True
            print('yes')
        else:
            return False
            print('no')

Upvotes: 1

Views: 60

Answers (3)

Mauricio Abreu
Mauricio Abreu

Reputation: 155

I also think you don't need a "break" statement after return. "return" will stop your function execution, not executing your "break" statement.

Upvotes: 0

iamauser
iamauser

Reputation: 11479

    return False
    print('no')

swap return and print lines similar to these ones:

    print('no')
    return False

Upvotes: 0

Michael0x2a
Michael0x2a

Reputation: 64118

When you call "return", the function immediately ends. Move all your print statements immediately before the "return" statements.

Alternatively, rearrange your code so that the prime-checking function contains no print statements. Instead, have another piece of code that uses it, and prints out "yes" or "no" depending on the output. This helps you keep the calculation code and the display code nicely clean and separate from each other.

Upvotes: 2

Related Questions