0

I'm trying to read a 2-D table but when I put the values and insert them into the index of the print function, I get an error.

The table is 6x6. Even if I write 1 as column and 1 as line I get the error again.

My code:

line = input('Do you wanna the object in the line:...>>>')
column = input('Do you wanna the object in the column:...>>>')
print ('Query line: Column:',column, ' Line:', line, line[int(line)][int(column)])
2
  • 1
    what is your 2d table called? Commented Jul 9, 2015 at 2:23
  • 1
    @JoshuaSalazar please write the complete code and define what 2-D table are you referring to ! Commented Jul 9, 2015 at 5:56

3 Answers 3

1

Since you have not given a name to the 2-D table, in my solution I assume it to be table.

table = [[], [], []]  # some 2-D array
line = int(raw_input("Enter the line: "))
column = int(raw_input("Enter the column: "))
print("Line:", line, "Column:", column, "Item:", table[line][column])

I hope you find this helpful.

Sign up to request clarification or add additional context in comments.

Comments

1

I am guessing your issue is that you are trying to do - line[int(line)][int(column)] , access the line string as a 2d table, you should give your 2d table's name , instead of line.

Example -

table[int(line)][int(column)] #if table is the name of the 2d table otherwise give 2d table's name instead of `table`

Comments

1

IndexError: string index out of range

This errors is simple to explain. Valid indices for a string str are in the range 0 to len(str) - 1 inclusive. You have supplied an index outside that range.

For instance, suppose we have

str = 'abc'

Then the following are valid: str[0], str[1] and str[2]. All other indices result in a runtime error.

You need to identify where in your code you supply an invalid index. It appears that would have to be where you write:

line[...]

you supply an invalid index.

Once you fix that you'll hit the next problem. That is that line[...] is a single character and cannot itself be indexed. So even when you fix the outer index, the inner indexing is always invalid.

I don't know what your code is meant to do so cannot tell you how to fix that next problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.