我一直试图让这段代码工作以将输入数字作为 2 到 49 之间的素数返回,但它所做的只是将大多数数字作为素数返回,即使它们不是......对于练习,我得到了无论如何,3、5 和 7 都是素数,所以请忽略那一点...
def prime(a):
if a < 2: return False
if a % 2 == 0: return False
if a == 3 or a == 5 or a == 7: return True
for n in range(3,int(a ** 0.5) + 1):
if a % n == 0: return False
if a % n != 0: return True
a = input("Enter a number between 1 and 49: ")
if prime(a) is False:
print a, " is not a prime number"
if prime(a) is True:
print a, " is a prime number"