【发布时间】:2020-05-10 06:09:44
【问题描述】:
总的来说,我是 Python 和 Stackoverflow 的新手,如果我的格式很糟糕并且我不擅长英语,我很抱歉。但是我的这段代码有问题。
print('Displays prime numbers from 1 to N.')
n = int(input('Please enter a value of n: '))
for n in range(1, n + 1):
if n >= 1:
for i in range(2, n):
if (n % i) == 0:
break
else:
print('They are',n,end=' ')
运行时的代码结果如下所示:
Displays prime numbers from 1 to N.
Please enter a value of n:40
They are 1 They are 2 They are 3 They are 5 They are 7 They are 11 They are 13 They are 17 They are 19 They are 23 They are 29 They are 31 They are 37
但我想要这样:
Displays prime numbers from 1 to N.
Please enter a value of n:40
They are 1 2 3 5 7 11 13 17 19 23 29 31 37
【问题讨论】:
标签: python python-3.x list dictionary printing