【问题标题】:Output is not in correct format输出格式不正确
【发布时间】:2019-10-10 13:57:51
【问题描述】:

我需要打印一个范围内的所有素数。但输出必须像 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)

【问题讨论】:

    标签: python count numbers output python-3.7


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      • 2016-12-30
      相关资源
      最近更新 更多