如何删除尾声,遵循结尾=','
Python&社区。导航不同的练习,我很难找到解决方案。我需要删除尾随的尾声,然后在使用End =','
任何智慧或指导都非常感谢!
low = 10000
up = 10050
for num in range(low, up + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num,end=',')
New to Python & the community. Navigating different exercises and I'm having trouble finding the solution. I need to remove the trailing, that follows when using end=','
Any wisdom or guidance is very appreciated!
low = 10000
up = 10050
for num in range(low, up + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num,end=',')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看来您正在尝试在给定范围内打印质数。可以说,混合质数的发现并打印它们是引起问题的原因。通过适当的分解,将不存在此问题:
作为积极的副作用,您现在可以在程序的其他部分重复使用Prime Generator,而该发电机不需要打印。
另请注意,检查所有数字最多
num
是不需要的 - 如果该数字为复合,则其中一个因素将较小或等于SQRT(NUM)。因此,更快的Prime发电机将是类似的:It looks like you're trying to print prime numbers in a given range. Arguably, mixing discovery of prime numbers and printing them is what causes the problem. With proper decomposition, this problem won't exist:
As a positive side effect, you can now reuse prime generator in other parts of the program which don't require printing.
Also note that checking all numbers up to
num
is not necessary - if the number is composite one of the factors will be less or equal to sqrt(num). So, a faster prime generator would be something like:尝试以下操作:
本质上,只需添加将在列表中输出的所有元素,然后使用Join函数将它们转换为字符串并打印它。
Try this :
Essentially, just add all the elements that will be outputted in a list and then use the join function to convert them into a string and print it.
解决
方案说明
由于我们无法确定哪个数字
num
循环将退出我们无法完全知道最终条件。但是我们可以找出起始数字。因此,基于该逻辑,而不是打印“”,最后,我们实际上是在开始时使用变量来维护的,如果我们在上述代码中打印了任何值,该值是由
> firstValuePrinted << /代码>。
sys.stdout.flush()
被调用,以确保控制台立即打印输出,并且不等待程序完成。注意:不要忘记
在文件开始时导入sys
。优点与其他答案相比,
如果要输出
num 变量连续,以免在编写值之前等待所有数字。
如果将所有值放入数组中,然后打印,则实际上需要大的内存来存储所有数字(如果
low
和up
是大)以及控制台将等到 完成后,才能执行打印命令。尤其是在处理质数时,如果
低
和up
变量相距甚远,例如low = 0
和up = 1000000
在打印任何输出之前,将需要很长时间才能对其进行处理。在上述实现的同时,它将在计算时打印。同时也确保没有其他“”,但最后打印出来。Solution
Explanation
Since we can't determine on which number
num
the loop will exit we can't exactly know the end condition. But we can figure out the starting number.So based on that logic, instead of printing the "," at the end we actually print it at the start by using a variable to maintain if we have printed any values or not in the above code that is done by
firstValuePrinted
.sys.stdout.flush()
is called to ensure the console prints the output right away and does not wait for the program to complete.Note: Don't forget to
import sys
at the start of the file.Benefits when compared to other answers
This type of implementation is useful if you want to output the
num
variable continuously so as to not have to wait for all the numbers to be calculated before writing the value.If you put all the values into an array and then print, you actually need large memory for storing all the numbers (if
low
andup
is large) as well as the console will wait till thefor loop
is completed before executing the print command.Especially when dealing with prime numbers if the
low
andup
variables are far apart likelow=0
andup=1000000
it will take a long time for it to be processed before any output can be printed. While with the above implementation it will be printed as it is being calculated. While also ensuring no additional "," is printed at the end.