python - 我想从列表中添加以下内容,但结果只是 concatinates

标签 python python-3.x list add

n的样本值为5

    n = input("Enter a No: ")
    n = "{0}+{0}{0}+{0}{0}{0}".format(n)
    n = n.split("+")
    a=n[0] 
    b=n[1]
    c=n[2] 
    n = (a + b + c) 
    print(n)

预期结果:615

最佳答案

你可以使用它。

n = input("Enter a No: ")
n = "{0}+{0}{0}+{0}{0}{0}".format(n)
out=sum([int(i) for i in n.split('+')])

如果您只想添加前三个元素,请使用它。

out_3=sum([int(i) for i in n.split('+')[:4]])

关于python - 我想从列表中添加以下内容,但结果只是 concatinates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60293479/

相关文章:

python - 如何将Python列表转换为pandas DataFrame :

r - 将变长元素列表转换为数据框

python - 如何从子字符串中获取列表中的索引值?

Python ThreadPoolExecutor 线程未完成

python - 使用 QDataWidgetMapper 立即更改模型

json - 在 Python 中使用 ISODate 解析 JSON 文件

list - 如何在 Haskell 中将三元组列表转换为一个三元组列表?

python - 如何使用光谱python处理多光谱栅格文件?

python - 将一列值与linux环境中的所有列进行比较

python - 如何使用 rxpython 使长时间运行的程序超时?