-1

I am doing some Python exercisses and for some reason my fucntion is returning to me opposite bool value. If statement under the function need to has "not" to work properly - can anyone explain me what am I doing wrong?

number = int(input("Enter numbers you want to check: "))


def dividers(number):
temp_tab = range(2, number)
divider = 0

for x in temp_tab:
    while number % x == 0:
        divider += 1
        if divider > 1:
            return False
        else:
            return True


if not dividers(number):
   print("It is a prime number")
else:
   print("It is not a prime number")
11
  • can you provide some input and expected output?
    – Matiiss
    Commented Mar 16, 2022 at 21:43
  • 1
    Because the logic of your dividers function is incorrect. You are always only ever returning after the first iteration (when x = 2)
    – Chad S.
    Commented Mar 16, 2022 at 21:46
  • Because your function is defined to return False if there are more than one divider hence the number is not prime.
    – re-za
    Commented Mar 16, 2022 at 21:46
  • What is the requirement? Do you want a function that checks if a number is prime and return True in that case? Or do you want a function that checks if a number has multiple dividers (hence it is not prime)?
    – re-za
    Commented Mar 16, 2022 at 21:47
  • Yes, I assumed that every number has at least 2 dividers so if there is one more it won't be prime number. Commented Mar 16, 2022 at 21:48

1 Answer 1

0

It seems like you want a function to check if a number is prime:

def is_prime(number):
    if number <= 1: return False

    for x in range(2, number):
        if number % x == 0:
            return False
    return True


number = int(input("Enter numbers you want to check: "))

if is_prime(number):
    print("It is a prime number")
else:
    print("It is not a prime number")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.