IndexError: list index out of range - python

后端 未结 2 2061
予麋鹿
予麋鹿 2021-01-06 17:34

I have the following error:

currency = row[0]
IndexError: list index out of range

Here is the code:

 crntAmnt = int(input(         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-06 18:05

    Try printing out the row. The convention for variable names in python are like_this and not likeThis. You might find the break keyword useful:

    for row in exch_reader:
        currency = row[0]
        if currency == crnt_currency:
            crnt_rt = row[1]
            break
    

    To only index the row when the row actually contains something:

    currency = row and row[0]
    

    Here row[0] is only executed if row evaluates to True, which would be when it has at least one element.

提交回复
热议问题