我有 2 个函数:一个“is_prime”将使用标志变量检查数字是否为素数。第二个调用第一个来检查给定的整数是质数、非质数还是负数。第二个函数大部分工作正常,但不会像我告诉的那样在整数大于 1 时调用第一个函数。 为什么最后一行文本没有调用第一个函数?
def is_prime():
#Start by setting prime integer to False
check = False
#Check if integer is a prime number or not by checking the factors of a flag variable
for i in range(2, int):
if (int % i) == 0:
#If a factor is found, set flag to True
check = True
#Leave the loop
break
if check:
#Print the integer is not prime if false
print("The integer",int,"is not prime.")
else:
#Print the integer is prime if true
print("The integer",int,"is prime.")
#Main function that accepts user input, and calls is_prime to find out if the number is prime or not.
def main():
global int
#Take the input from the user and make it an integer
int = int(input("Please provide an integer:"))
if int < 0:
#If the integer is negative, print the integer being negative
print("The integer",int,"is negative")
#If the integer is not negative, it must check that it isnt between 0 or 1 or it will break the "prime number checking" function.
elif int == 1:
#If the integer is 1, print that the integer is not prime
print("The integer",int,"is not prime.")
elif int > 1:
is_prime
main()