【发布时间】:2014-02-01 15:40:58
【问题描述】:
您好,我是编程和 python 方面的新手,我有一个无法完成的作业。我必须编写一个 Python 程序来计算和打印前 200 个素数。输出必须用标题格式化,并且素数必须打印在 5 个正确对齐的列中。到目前为止我已经使用了这个代码:
numprimes = raw_input('Prime Numbers ')
count = 0
potentialprime = 2
def primetest(potentialprime):
divisor = 2
while divisor <= potentialprime:
if potentialprime == 2:
return True
elif potentialprime % divisor == 0:
return False
break
while potentialprime % divisor != 0:
if potentialprime - divisor > 1:
divisor += 1
else:
return True
while count < int(numprimes):
if primetest(potentialprime) == True:
print potentialprime
count += 1
potentialprime += 1
else:
potentialprime += 1
但我在单个列中得到结果。我怎样才能把它分成 5 行?
【问题讨论】:
-
请粘贴您的实际代码。然后突出显示它并单击
{}按钮。内容在发布时出现乱码。