简体   繁体   English

Python:获取列表索引超出范围错误

[英]Python:Getting list index out of range error

Getting list index out of range error in line amount=int(values 1 ),as far as I can see ,I am not violating any index ranges. 据我所知,在行数量=整数(值1 )中获取列表索引超出范围错误,我没有违反任何索引范围。 I have checked these 我检查了这些
Python error - list index out of range? Python错误-列表索引超出范围?
List index out of range? 列表索引超出范围?
list index out of range 列表索引超出范围
but they are of not much use. 但它们用处不大。

netAmount = 0
while True:
    s = raw_input()
    if not s:
        break
    values = s.split(" ")
    operation = values[0]
    amount = int(values[1])  
    if operation=="D":
        netAmount+=amount
    elif operation=="W":
        netAmount-=amount
    else:
        pass

print netAmount

**edited-After printing after the split method print is working fine but error still persists **编辑-分割方法打印后打印正常,但错误仍然存​​在 输出量

You must add the test before the split to avoid accessing the list out of range: 您必须在拆分之前添加测试,以避免访问超出范围的列表:

netAmount = 0
while True:
    s = raw_input()
    if not s or s=="0":
        break
    values = s.split(" ")
    operation = values[0]
    amount = int(values[1])
    if operation=="D":
        netAmount+=amount
    elif operation=="W":
        netAmount-=amount
    else:
        pass

print netAmount

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM