So my code to test a prime number is as follows:

 PrimeTest():
    test = input ( "What number would you like to test? " )
    test = int(test)

    true = 0
    divisor = 0

    for divisor in range (2, 10):
        if ( (isinstance( test/divisor, int )== True) &  (test/divisor == int < 2) ):
            print ("This number is divisible by " + str(divisor) )

        if ( (test != divisor) & (isinstance( test/divisor, int)== False) ):
            true = true + 1
            if ( true == 8 ):
                print ("This number is prime.")

PrimeTest()

The value output always have a float type, even though it is an interger (i.e. 2.0). How do I fix that?

Is not syntax error but does not make any kind of sense ot do bitwise arithmetic with boolean:

(isinstance( test/divisor, int )== True) &  (test/divisor == int < 2)

You should be using % (modulus operator or remainder) and integer values.

Edited 3 Years Ago by pyTony

kind

Is not syntax error but does not make any kind of sense ot do bitwise arithmetic with boolean:

reason for that is that I need it to be an interger and not a float, because you need to test if the number is divisible by a whole number. Ex. (4/5 != int) but (5/5 = int = true) Make sense?

No, it does not, 4/5 is float number and int is of type 'type', the other formula of yours is assignment syntax error.

Edited 3 Years Ago by pyTony

So how do I change that without messing up the function?

Edited 3 Years Ago by bnn678: mistake

def prime_test():
    test = int(input("What number would you like to test? "))
    assert 0 < test < 11 * 11
    for divisor in range(2, 8): #highest prime under eleven is 7
        if divisor < test and test % divisor == 0:
            print("This number is divisible by " + str(divisor))
            break
    else:
        print('This number is prime')

prime_test()

By the way, this test is also supposed to test what a number is divisible by so that is part of the reason why it is not simpler.
Thanks for your help!

Edited 3 Years Ago by bnn678: to correct

I'm curious what line 3 does though, could you explain it?

Also, I'm trying to make this a universally usable method. So for example if you use that code and enter a number like 9997, there is an assertion error

Edited 3 Years Ago by bnn678: correction

Yes, that loop is only applicable until square of next prime after the limit, you should calculate the needed limit or break out of loop when divisor exceed reasonable limit. Are you trying to find smallest factor or all divisors? I havebwritten code snippet for both of those here.

all divisors
though all divisors in the range of 2-10 is perfectly fine

Edited 3 Years Ago by bnn678: corretion

I also want to be able to enter a test number to test it for divisors. So for example the program would test for a varible. Then would tell you what that code is divisible by.

Recently found out there is a GCD function on Python, was easy from there

This question has already been answered. Start a new discussion instead.