I'm new to python, and I can't see why my for loop is ignoring the if statement and saying every number is a prime number. My code is as such.
def prime_number(number):
sqrt_prime = math.sqrt(number)
for x in (2,sqrt_prime-1):
if isinstance((number/x), int) and x <= sqrt_prime-1:
con_num = str(number)
print(con_num,end=' ');print('is not a prime number.')
break
else:
con_num = str(number)
print(con_num,end=' ');print('is a prime number.')
It could probably be a lot more streamlined, just want to know why it isn't either outputting true for the isinstance or that x is smaller or equal to the sqrt of the number - 1. Thanks in advance.
isinstanceit may be float is you're using python3number/xwill always be a float in python3, so your condition will not be satisfied(The same opinion as @neilharia7)(2, sqrt_prime-1)to half.if number % x == 0instead of using/though personally I would be cautious about0vs0.0and getting incorrect answers from rounding. You might also tryif number / x == number // x:.