2017-05-01 1 views
0

리스트 인덱스가 라인 수 = int (값 1)의 범위를 벗어났습니다. 인덱스 범위를 위반하지 않았습니다. 나는 이것들을 검사했다.
Python error - list index out of range?
List index out of range?
list index out of range
그러나별로 유용하지 않다.파이썬 :리스트 인덱스가 범위를 벗어났습니다. 에러가 발생했습니다.

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 

** 편집-후 인쇄가 잘 작동하지만 오류가 계속 Output

+1

'vaues = s.split ("")'다음에'print values'를 사용하여 결과를 공유 할 수 있습니까? 또한 '금액'에 대해서도 동일하게하십시오 – Malik

+0

어떤 입력을하고 있습니까? 하나 이상의 단어가있는 것이 작동해야합니다. – zondo

+0

''raw_input''에 어떤 입력이 여러분에게 오류를주고 있습니까? 언뜻보기에는''D 10''이나''W 20''을 치면된다. – Grimmy

답변

2

당신이 범위를 벗어나 목록에 접근 피하기 위해 분할하기 전에 테스트를 추가해야 지속 분할 방법 후 인쇄 :

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 
+0

s와 s == "0"은 같은 것이 아닙니다. – Bing

+0

s가 ""인지 확인하고 s가 "0"이면 의미가 없습니다 ... – Neil

+0

좋아, 이제 알았어. 실제로 C/C++에서 일 했었어. 그래서 나는 그걸 비유하고 있었어. – Bing

관련 문제