python - 输出格式不正确
问题描述
我需要打印一个范围内的所有素数。但是输出必须像 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)
解决方案
将整数添加到列表中,然后在打印时使用
print(f"The prime number(s) in this range are {','.join(integer_list)}")
推荐阅读
- javascript - 仅在这些搜索到的元素上搜索元素并设置分页
- jquery - jquery中的计时器恢复问题
- ios - iOS 应用程序使用上次打开的 ViewController 屏幕重新启动
- html - 由于在同一元素上的多个指令中使用了相同的鼠标事件,指令没有响应
- docker - 计算机重新启动时 Docker 不会运行
- bash - 如何在由分隔符分隔的值周围添加双引号?
- android - org.gradle.api.UncheckedIOException:java.util.zip.ZipException:重复条目:META-INF/MANIFEST.MF
- http-live-streaming - HLS 为什么选择 TS 容器?
- c - 需要理解这个If语句
- vue.js - 有没有办法按 vuejs 中输入框内的内容进行排序?