一切似乎都正常运行,除了在运行程序并且数字不是素数时我仍然得到None输出。如果没有“无值”打印,则“ IS PRIME”可以正常工作
def is_prime():
your_guess = int(input("Choose a number to find out if it's prime..."))
div = 2
is_prime = True
while div < your_guess:
if your_guess % div == 0:
is_prime = False
print ("The Number is not Prime")
break
div += 1
else:
if is_prime == True:
return ("The number is Prime!")
print (is_prime())
答案 0 :(得分:0)
def is_prime():
your_guess = int(input("Choose a number to find out if it's prime..."))
div = 2
is_prime = True
while div < your_guess:
if your_guess % div == 0:
is_prime = False
print ("The Number is not Prime")
break
div += 1
else:
if is_prime == True:
print ("The number is Prime!")
return is_prime
尝试此操作并检查程序的逻辑。没有返回的原因是因为您在if(is_prime == True)之后使用了return语句。记住return和print是两件事。
答案 1 :(得分:0)
一个函数应该为您指定的两个条件返回一些内容。
def is_prime():
your_guess = int(input("Choose a number to find out if it's prime..."))
div = 2
is_prime = True
while div < your_guess:
if your_guess % div == 0:
is_prime = False
return "The Number is not Prime"
div += 1
else:
if is_prime == True:
return ("The number is Prime!")
print(is_prime())