0

Uses python3

import sys

input = sys.stdin.read() 
tokens = input.split()
a = int(tokens[0]) 
b = int(tokens[1]) 

print(a + b)

I am trying to compile this simple code in Python. HOwever it gives me Index Error:List Index out of range. I just need to compile this code and then feed 2 digits to it at input line.

2 Answers2

1

.split() splits on space. if your numbers have no space between it won't work, as they both will be in a single element (tokens[0])

Demonstration:

>>> '12'.split()
['12']
>>> '1 2'.split()
['1', '2']
nosklo
  • 217,122
  • 57
  • 293
  • 297
0

Try this:

import sys

inputed = sys.stdin.read() 
tokens = str(inputed)
a = int(tokens[0]) 
b = int(tokens[1]) 

print(a + b)
whackamadoodle3000
  • 6,684
  • 4
  • 27
  • 44