简体   繁体   English

如何使我的for循环仅打印一次,而不是为每个i值打印一次

[英]how to make my for loop print only once instead of for each value of i

I am trying to create a function that checks if a given number as a parameter is prime or not. 我正在尝试创建一个函数,用于检查给定数字作为参数是否为素数。 I decided to do this with a for loop but it keeps printing that the number is not prime for certain dividers and that it is prime for other numbers. 我决定使用for循环来执行此操作,但它会不断输出该数字对于某些除法器而言不是素数,而对于其他数字而言则是素数。 Here is my code: 这是我的代码:

def prime(n):
    for i in range(2, n):
        if n%i == 0:
            print (n," is not prime because it is divisible by ", i)

        else:
            print (n, " is a prime number")


prime (15)

this is the output: 这是输出:

15  is a prime number
15  is not prime because it is divisible by  3
15  is a prime number
15  is not prime because it is divisible by  5
15  is a prime number
15  is a prime number
15  is a prime number
15  is a prime number
15  is a prime number
15  is a prime number
15  is a prime number
15  is a prime number
15  is a prime number

So instead of giving this as an output, I would like to immediately recognise that the number is not prime if it has a divider other than 1 and itself. 因此,我不想立即将其作为输出,而是想立即认识到,如果该数字除以1以外还没有除数,则它不是质数。 If the number doesn't. 如果没有。 Kind of like this: 有点像这样:

#if number if prime
15 is a prime number

#if number is not prime
15 is not a prime number because it is divisible by 3

By return ing, the function stops once it recognizes that a number is not prime. 通过return ,函数将在识别出数字不是质数时停止。

def prime(n):
    for i in range(2, n):
        if n%i == 0:
            print (n," is not prime because it is divisible by ", i)
            return 
    print (n, " is a prime number")
    return

The benefit of this is once the expression n%i == 0 evaluates to true, it prints, and it returns, stopping further unnecessary evaluations. 这样的好处是,一旦表达式n%i == 0计算结果为true,它将打印并返回,从而停止进一步的不必要的计算。

Try this 尝试这个

def prime(n):
    for i in range(2, n):
        if n%i == 0:
            print (n," is not prime because it is divisible by ", i)

prime (15)

I would try something like: 我会尝试类似的东西:

def prime(n): 
    isprime = True
    for i in range(2, n): 
         if n%i == 0:
              print (n," is not prime because it is divisible by ", i)
              isprime = False
              break
    if isprime:
         print (n, " is a prime number")
prime (15)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确保我只打印一次? - How to make sure I only print once? 如何使嵌套的循环打印仅在python中一次 - how to make nested for loop print only once in python 如何使try函数仅打印我的消息一次 - How to make the try function only print my message once 如何在满足条件而不是无限的情况下仅打印一次 else 或 if 语句而不中断 - How can I print else or if statement only once when the condition is meet instead of infinite without breaking 只获取一次循环返回的值到 print() - Getting the Value returned from a Loop to print() only once 如何在python中制作循环x,将值赋予变量,并每次打印此值? - How to make a loop x in python, attribute a value to a variable and print this value each time? 如何使程序完成时绘制所有矩形,而不是一次绘制一次? - How do I make it so that my program draws all of my rectangles when it completes, instead of once at a time? 我的代码为每一行打印“未找到”,如果搜索不成功,我如何让它只打印一次“未找到”? - My code prints “Not Found” for every line, how do i get it to only print “Not Found” once if the search is unsuccessful? 如何使 re.finditer 只返回每行一次 - How do I make re.finditer only return each line once 按下鼠标按钮后,如何使程序不会无限循环运行 - How can I make my program not run in an endless loop once my mouse button is pressed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM