john john - 1 month ago 6
Python Question

Error while accepting inputs from command line in python

I tried to take an input from the command line by passing a file and storing it in an array.
I know my input is two words separated by white space, so I tried

import sys
for line in sys.stdin:
a=[]

a=line.split() #to split the 2 words separated by white space into 2 list elements

print a # just to debug my code

edges(a[0], a[1]) #calling the function edges


To this I m getting an error as:-

edges(a[0], a[1])
IndexError: list index out of range


Can someone suggest what am I doing wrong ?

Thank you

Answer

Try specifying the index into which you are inputting the word. This will keep the list/array from overflowing and causing "index out of bounds" error.

EDIT I see you are trying to store the first element as the key and then value. Please use a counter such as 'k' and 'v' to keep track and avoid overflows.

Hope this helps,