函数的相反 return
opposite return of the function
我正在做一些 Python 练习,由于某种原因,我的功能正在返回相反的 bool 值。如果函数下的语句需要“不”才能正常工作 - 谁能解释我做错了什么?
number = int(input("Enter numbers you want to check: "))
def dividers(number):
temp_tab = range(2, number)
divider = 0
for x in temp_tab:
while number % x == 0:
divider += 1
if divider > 1:
return False
else:
return True
if not dividers(number):
print("It is a prime number")
else:
print("It is not a prime number")
您似乎想要一个函数来检查数字是否为质数:
def is_prime(number):
if number <= 1: return False
for x in range(2, number):
if number % x == 0:
return False
return True
number = int(input("Enter numbers you want to check: "))
if is_prime(number):
print("It is a prime number")
else:
print("It is not a prime number")
我正在做一些 Python 练习,由于某种原因,我的功能正在返回相反的 bool 值。如果函数下的语句需要“不”才能正常工作 - 谁能解释我做错了什么?
number = int(input("Enter numbers you want to check: "))
def dividers(number):
temp_tab = range(2, number)
divider = 0
for x in temp_tab:
while number % x == 0:
divider += 1
if divider > 1:
return False
else:
return True
if not dividers(number):
print("It is a prime number")
else:
print("It is not a prime number")
您似乎想要一个函数来检查数字是否为质数:
def is_prime(number):
if number <= 1: return False
for x in range(2, number):
if number % x == 0:
return False
return True
number = int(input("Enter numbers you want to check: "))
if is_prime(number):
print("It is a prime number")
else:
print("It is not a prime number")