【发布时间】:2020-11-12 11:54:37
【问题描述】:
amount = int(input())
for i in range(amount):
x = input().split()
y = ([int(i) for i in x])
z = y[0] + y[1]
print("Result = ",z)
if i input amount = 3
and x = 2 3
4 5
7 8
上面的代码将打印出来:
Result = 6
Result = 9
Result = 15
但我希望它像这样打印:
Result = 6 9 15
所以我的想法是将 sum 的每个结果保存到不同的变量并打印它们,但我不知道该怎么做。我知道请告诉我。或者如果你有更好的主意。
【问题讨论】:
-
要么打印不带换行符,要么存储在列表中并打印合并结果...
-
欢迎来到 Stackoverflow!提出问题时,请尝试正确格式化您的代码并良好地组织您的文本。例如,不要在代码 sn-p 中放置一些描述所需输出的文本。
-
只是为了好玩,将整个代码作为一个单行代码:
print(f"Result = {' '.join(map(str, [sum(map(int, input().split()[:2])) for _ in range(int(input()))]))}") -
"所以我的想法是将 sum 的每个结果保存到不同的变量中" 您是否尝试将
x输入中的每个整数保存到不同的变量中?为什么不?你做了什么?你能想出一种再次应用该技术的方法,以便将所有z值放在一起吗?
标签: python python-3.x loops for-loop sum