如何使用python拆分整数并对拆分的整数求和?

时间:2015-06-20 18:28:15

标签: python

我已经用python语言给出了一个任务,我应该将给定的数字分成两半,并将两半中的每一个相加。例如:

input = 1234
output must be 1 + 2 = 3 and 3 + 4 = 7. means 3 & 7.

说明:我必须将给定输入1234拆分为12和34。

然后我必须将1 + 2和3 + 4相加,最终输出将为37。

PS:这是我自己尝试的。我是python的新手。

#T = int(raw_input())
#while T > 0 :
#    input = raw_input()
#    i = []
#    i.append(input)

1 个答案:

答案 0 :(得分:0)

data = input()
# Divide the list
div = len(data)//2
lst = []
# Add both halves to the list
lst.append(data[:div])
lst.append(data[div:])
tot = ""
# Loop through the list and add the sum as a string
for i in lst:
    tot += str(int(i[0])+int(i[1]))
# Print the final string
print(tot)