我使用以下代码解决了 python 中的问题:
T = int(raw_input())
C=[]
for x in range(T):
C[x]=int(raw_input())
res=[]
for x in range(T):
res[x]=2**C[x]+2*C-1
for x in range(T):
print "%d\n",(res[x])
出现了这个错误:
Traceback (most recent call last):
File "C:\Users\ACER\Documents\works\source code\python practice\Test1.py", line 4, in <module>
C[x]=int(raw_input())
IndexError: list assignment index out of range
请问可以解决这个错误吗
最佳答案
您的列表是空的。 perl 会自动扩展数组,但 python 不会。
C.append(int(raw_input()))
相反。
关于python - 索引错误: list assignment index out of range in Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25407328/