我对 python 比较陌生,一直在研究一个问题,以找到两个输入之间的质数。我有一个有效的解决方案(也通过在线搜索有所帮助),但我不确定为什么 else
下面显示的语句不应与 if
处于相同的选项卡设置中陈述。但是,如果是这样,它就不能正常工作。任何人都可以为我澄清这一点吗?
我的代码在这里:
n1 = int(input("Enter the lower number: "))
n2 = int(input("Enter the higher number: "))
for num in range(n1, n2 + 1):
if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num)
最佳答案
你看到的是 Python(相当独特) for:else:
pattern , 在 break
时执行某事在for
内没有遇到套房:
When the items are exhausted, the suite in the
else
clause, if present, is executed, and the loop terminates.
Abreak
statement executed in the first suite terminates the loop without executing theelse
clause’s suite. [...]
关于python - 试图理解 Python 中的缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59156701/