【发布时间】:2021-11-19 17:47:36
【问题描述】:
我在尝试使用 while 函数进行循环时遇到了困难。 基本上我希望代码向我显示从 0 到我在输入中写入的数字的所有素数。 然后,问一个问题我是否想再做一次(如果是,从头开始重复代码)还是不退出。 我现在如何得到它只是无限重复最后的结果。 我在 while 循环中在线找到的所有结果并没有真正解释如何重复代码的某个部分。 在这方面我根本没有受过一点教育,所以如果这是一个愚蠢的问题,请原谅我。
# Python program to print all primes smaller than or equal to
# n using Sieve of Eratosthenes
def start(welcome):
print ("welcome to my calculation.")
value = input("number?:\n")
print(f'you have chosen: {value}')
value = int(value)
def SieveOfEratosthenes(n):
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p ** 2, n + 1, p):
prime[i] = False
p += 1
prime[0] = False
prime[1] = False
print("Primary numbers are:")
for p in range(n + 1):
if prime[p]: print(p)
# driver program
if __name__ == '__main__':
n = value
SieveOfEratosthenes(n)
pitanje = input("do you want to continue(yes/no)?")
while pitanje == ("yes"):
start: SieveOfEratosthenes(n)
print("continuing")
if pitanje == ("no"):
print("goodbye")
强文本
【问题讨论】:
标签: loops while-loop repeat