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

    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.")


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

Recommended Answers

All 14 Replies

I get errors on lines 3, 11, and 14 so I can't get any output to test for a float.

what version do you have? I have 34

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.


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.

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

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))
        print('This number is prime')


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!

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.