gpt4 book ai didi

Python,在结束后开始一个新行= ""

转载 作者:行者123 更新时间:2023-11-30 22:39:57 24 4
gpt4 key购买 nike

目前,我有以下代码:

for i in range(lower_limit, upper_limit+1): 

for j in range(0,len(prime_number)):
for k in range(0 + j,len(prime_number)):
if i == prime_number[j] + prime_number[k] and i % 2 == 0:

print(i, "=", prime_number[j], "+", prime_number[k])

打印:

10 = 3 + 7 10 = 5 + 5 12 = 5 + 7 14 = 3 + 11 14 = 7 + 7 

我需要的结果如下:

10 = 3 + 7 = 5 + 512 = 5 + 714 = 3 + 11 = 7 + 7

我知道我必须以某种方式使用 end = "",但它会在一行中打印所有数字。我该怎么办?

最佳答案

使用end= "",并在外循环末尾使用一个空的print()来结束该行。

例如:

>>> for i in range(3):
... print("foo", end = " ")
... print("bar", end = " ")
... print()
...
foo bar
foo bar
foo bar

专门针对您的情况:

for i in range(lower_limit, upper_limit+1): 
print(i, end = " ")
for j in range(0,len(prime_number)):
for k in range(0 + j,len(prime_number)):
if i == prime_number[j] + prime_number[k] and i % 2 == 0:

print("=", prime_number[j], "+", prime_number[k],end = " ")
print()

关于Python,在结束后开始一个新行= "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42993126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com