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.

1
  • Show how you run it. There's no error here and Python doesn't compile in the sense you mean. Commented Jun 20, 2018 at 19:36

2 Answers 2

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']
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

import sys

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

print(a + b)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.