首页 > 解决方案 > 输出格式不正确

问题描述

我需要打印一个范围内的所有素数。但是输出必须像 1 行一样,由于某种原因,我不能使用 list 或 .append 甚至 .join。例如,范围是 8 到 20,输出将是:"The prime number(s) in this range are 11, 13, 17, 19

我现在的代码是:

start_range = int(input("Enter the first number in the range"))
end_range = int(input("Enter the number in the end of the range"))

for integer in range(start_range, end_range + 1):
        count = 0
        for prime in range(2, integer+1):#This loop is from part 1 and will tell if the number is prime or not
            if (integer % prime) == 0:
                count = count + 1
                if count > 2 and count != 1:
                    break
        else:
         print("The prime number(s) in this range are", integer)

标签: pythoncountnumbersoutputpython-3.7

解决方案


将整数添加到列表中,然后在打印时使用

print(f"The prime number(s) in this range are {','.join(integer_list)}")

推荐阅读