添加具有先前位置的循环的当前位置

时间:2015-10-14 11:01:07

标签: python

这可能是个愚蠢的问题但是 我有一个循环,其中n是用户输入

n = input("enter number"):
for i in range(n):
    a = input("enter a value for each n")

现在我需要添加a的当前位置和

的先前位置
    c = a[n] + a[n-1]

我该怎么做?

假设n = 5且a = 1,2,3,4,5

1 个答案:

答案 0 :(得分:1)

if you want your script to add numbers, you need to store the sum in a variable

n = int(input("enter number"))
b = 0 # store sum here
for i in range(n):
    a = int(input("enter a value for each n"))
    b = b + a
    print(b)

enter number4
enter a value for each n2
2
enter a value for each n3
5
enter a value for each n4
9
enter a value for each n6
15