如何在Python程序中只获取结果的单个输出?

huangapple go评论68阅读模式
英文:

How to get only a single output of the result in python program?

问题

以下是代码部分的翻译:

我正在编写一个用Python来识别质数的代码以下是代码

n = int(input("输入任意整数"))
for i in range(2, n, 1):
   if n % i == 0:
       print(n, " 不是质数")
       break
   else:
       print("质数")

但问题是,每当else条件激活时,我得到"质数"作为输出。我也附上了输出结果。

查看图片描述

我想要在数字是质数时只输出一个"质数"。如何才能实现这一点?

我尝试在else内部使用continue

n = int(input("输入任意整数"))
for i in range(2, n, 1):
   if n % i == 0:
       print(n, " 不是质数")
       break
   else:
       continue

如果数字是质数,就没有输出。但我想要在数字是质数时输出"质数"。

然后我尝试使用for else条件。

虽然它确实有效,但我想知道如何阻止先前代码多次输出"质数"。

英文:

I was writing a code in python to identify prime number. Here is the code

n=int(input("Enter any integer"))
for i in range(2,n,1):
   if n%i==0:
       print(n, " is not a prime number")
       break
   else:
       print("Prime number")

But the problem is that I am getting "Prime number" as an output for each time the else condition is activated. I am attaching the output results too.

enter image description here

I want to have a single "Prime number" written as an output if the number is prime. How can I get there?

I tried to use continue inside the else .

n=int(input("Enter any integer"))
for i in range(2,n,1):
   if n%i==0:
       print(n, " is not a prime number")
       break
   else:
       continue

If the number is prime there is no output. But I want "Prime number" written as my output if the number is prime.

Then I tried to apply for else condition.

It did work though but I want to know how to stop the multiple "prime number" printing of my previous code.

答案1

得分: 1

你可以这样修复它:

n = int(input("输入任意整数"))

is_prime = True
for i in range(2, n):
    if n % i == 0:
        is_prime = False
        break

if is_prime:
    print(n, " 是一个质数")
else:
    print(n, " 不是一个质数")

这会在找到任何除数时将变量 is_prime 设置为 False,立即退出循环。如果循环完整运行,那么没有找到除数。

请注意,可以通过以下几种方式加快速度:

  1. 在检查 2 是否是除数之后,可以跳过其余的偶数,仅检查奇数除数。
  2. 循环不需要一直运行到 n-1。一旦 i 超过了 n 的平方根,并且没有找到除数,那么 n 是质数,循环可以退出。

但是通过这个修复应该可以正常运行。

英文:

You can fix it like this:

n=int(input("Enter any integer"))

is_prime = True
for i in range(2,n):
    if n%i==0:
        is_prime = False
        break

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

This sets the variable is_prime to False if any divisor is found, exiting the loop immediately. If the loop runs to completion, then no divisor was found.

Note that this could be sped up in a couple ways:

  1. After checking if 2 is a divisor, you could skip the remaining even numbers and only check for odd divisors.
  2. The loop doesn't need to run all the way up to n-1. Once i exceeds the square root of n, if no divisor has been found, then n is prime and the loop can exit.

But it should function correctly with this fix.

答案2

得分: 0

for i in range(2,n,1): if n%i==0: print(n, " is not a prime number") exit() print(f"{n} is a prime number")

英文:

Every time n%i is not equal to 0, the else statement runs.
To fix this, you can remove the else statement entirely and just stop the code after the if statement runs. For example, you can do this like this:
for i in range(2,n,1):
if n%i==0:
print(n, " is not a prime number")
exit()
print(f"{n} is a prime number")

答案3

得分: 0

以下是已翻译的内容:

有很多方法可以做到这一点(比如提前终止程序,或者保存到变量以后检查等等),但我认为你正在寻找的是for循环的else子句。

n=int(input("输入任何整数"))
for i in range(2,n,1):
    if n%i==0:
       print(n, " 不是质数")
       break
else:
   print("质数")

是的,这与你的代码几乎一样,只是else不缩进。else语句在for循环之后,基本上意味着“如果循环没有中断,就运行这个,否则跳过它”。

英文:

There are a bunch of ways to do this (like terminating the program early, or saving to a variable to check on later, etc), but i think the one you are looking for is the for else clause.

n=int(input("Enter any integer"))
for i in range(2,n,1):
   if n%i==0:
       print(n, " is not a prime number")
       break
   else:
       print("Prime number")

and yes, this is exactly like your code but with the else unindented. That Else statment that comes after the for loop basically means "Run this if the loop didn't break, and skip it otherwise".

huangapple
  • 本文由 发表于 2023年3月7日 03:54:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655262.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定