英文:
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.
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
,立即退出循环。如果循环完整运行,那么没有找到除数。
请注意,可以通过以下几种方式加快速度:
- 在检查 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:
- After checking if 2 is a divisor, you could skip the remaining even numbers and only check for odd divisors.
- The loop doesn't need to run all the way up to
n-1
. Oncei
exceeds the square root ofn
, if no divisor has been found, thenn
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".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论