Math 无限重复打印语句

Math 无限重复打印语句,math,printing,repeat,infinite,Math,Printing,Repeat,Infinite,我目前正在从事一个项目,其中要求用户输入一个数字,程序将确定该数字是否为素数。这里的问题是print语句在执行后会重复自身,停止它的唯一方法是在程序运行时终止它 以下是该程序的代码: print("Here, you will be asked to enter a number and the program will determine whether said number is prime. ") print ("For the sake of simplicity,

我目前正在从事一个项目,其中要求用户输入一个数字,程序将确定该数字是否为素数。这里的问题是print语句在执行后会重复自身,停止它的唯一方法是在程序运行时终止它

以下是该程序的代码:

print("Here, you will be asked to enter a number and the program will 
       determine whether said number is prime. ")

print ("For the sake of simplicity, we'll only be using numbers that are 
        below 100. ")

Number = int (input("Please enter a number and we will determine if the 
number you entered if prime. "))

for Number in range (1,98):
    for y in range (1,98):
        if Number %y!=0:
            print ("This number is, in fact, a prime number. ")
        else:
            if Number %y==0:
                print ("This number is not prime.")

要使一个数成为素数,它只能被自身和1整除。 所以,你需要检查的是[2,n]之间的数字,如果它可以被任何数字整除,那么它不是素数,你需要打破for循环

如果for循环在到达末尾时中断,那么这意味着[2,n)之间的任何数字都不是该数字的因子,因此该数字是素数。我们使用isPrime布尔标志检查循环是否自然中断

该程序可能不会编译,因为我没有运行它,但逻辑是正确的

print ("Here, you will be asked to enter a number and the program will determine whether said number is prime. ") 
print ("For the sake of simplicity, we'll only be using numbers that are below 100. ")
isPrime = True
Number = int (input("Please enter a number and we will determine if the number you entered if prime. "))

for y in range (2,Number): 
    if Number%y==0 and Number!=y: 
       isPrime = False
       print ("This number is not prime.")
       break
if isPrime:
       print "Number is prime"

请使用有效格式更新您的问题,并提及您使用的语言。