以下是在给定范围内打印素数的基本程序代码。我希望在列表中排列素数而不是每行打印一次。
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
答案 0 :(得分:1)
您应该附加到列表而不是打印数字:
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
print("Prime numbers between", lower, "and", upper, "are:")
prime_numbers = []
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
prime_numbers.append(num)
print(prime_numbers)
答案 1 :(得分:0)
lower = int(input("Enter lower range: "))
upper = int(input("Enter upper range: "))
prime_list = list()
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
prime_list.append(num)
print(prime_list)
创建一个空列表prime_list = list()
并将素数附加到该列表。最后打印列表print(prime_list)
答案 2 :(得分:0)
{{1}}