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?

Re: Prime Number Test 80 80

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

Re: Prime Number Test 80 80

what version do you have? I have 34

Re: Prime Number Test 80 80

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.

Re: Prime Number Test 80 80

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?

Re: Prime Number Test 80 80

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

Re: Prime Number Test 80 80

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

Re: Prime Number Test 80 80
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()
Re: Prime Number Test 80 80

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!

Re: Prime Number Test 80 80

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

Re: Prime Number Test 80 80

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.

Re: Prime Number Test 80 80

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

Re: Prime Number Test 80 80

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.

Re: Prime Number Test 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.