Ilya Lopukhin
Ilya Lopukhin

Reputation: 692

Can't turn element of a list into an integer

I got a list in my code, it looks like. L = ['Nickname', '35'] When i try to i = int(L[2]) it catches an exception

exceptions.ValueError: invalid literal for int() with base 10: ''

What am I doing wrong?

      namesplitted = line.split()
      lnum += 1 
      truename = namesplitted[0]
      kills = namesplitted[1]
      print kills
      >>> 34
      i = int(kills[1])

Upvotes: 0

Views: 90

Answers (1)

Sukrit Kalra
Sukrit Kalra

Reputation: 34493

It's because your number '35' is located at L[1]. List Indices start from 0 in Python. So the first element is L[0], the second is L[1] and so on.

Your list is probably L = ['Nickname', '35', '']

>>> L = ['Nickname', '35', '']
>>> int(L[2])

Traceback (most recent call last):
  File "<pyshell#142>", line 1, in <module>
    int(L[2])
ValueError: invalid literal for int() with base 10: ''
>>> int(L[1])
35

Upvotes: 2

Related Questions