访问python中的列表项

时间:2015-12-15 20:23:53

标签: list python-2.7

以下代码正常工作,正如预期的那样(在数组中打印元素):

arr = []
n = int(raw_input())
for i in range (0,n):
    num = int(raw_input())
    arr.append(num)

for i in range(0,n):
    print arr[i]

但是,当我尝试以相反的顺序打印数组元素时,

arr = []
n = int(raw_input())
for i in range (0,n):
    num = int(raw_input())
    arr.append(num)

for i in range(0,n):
    print arr[n-i] #notice this line

这给了我:IndexError: list index out of range

0 个答案:

没有答案