An other error on Python

In summary, the programmer was trying to assign an element in a list (representing a matrix) to a variable, and received an error when they tried. When n is 1, A and B are no longer nested lists, and the subscripting fails because A[0][0] doesn't exist.
  • #1
azerty12
15
0
Hi all!

I'm learning programming on Python.
Currently, I'm trying to implement a recursive algorithm using a divide and conquer method to compute a product of matrices.

Yet an error occurs when I try to assign an element in a list (representing a matrix)

I can't understand where the problem comes from, since I tried on the shell to enter a matrix exactly as in the program and assign its values (C[0][0]=...) and it works.
But when I try to execute the program, the following error is raised:line 42, in sqmat_mult_rec
C[0][0]=A[0][0]*B[0][0]
TypeError: 'int' object is not subscriptable
Any idea?

(code attached)
 

Attachments

  • Python_Error.txt
    1.1 KB · Views: 532
Last edited:
Technology news on Phys.org
  • #2
Why don't you try printing out A, B and C right before the error occurs? The 'not subscriptable' error usually means that you are trying to look at a subscript that doesn't exist.
 
  • #3
I tried to and actually each of the objects I try to assign exist...
 
  • #4
I'm sure they exist, but are they nested lists? When you write A[0][0], it means A must be of the form [[something]]. If A is a simple variable (which is what the message implies) or a simple list [], then you will get an error when you try to access A[0][0]. What does the print out of A and B look like?
 
  • #5
Try adding
Code:
print(type(A))
print(type(B))
before that call to see what type is getting passed. From the error, A or B are integers.
 
  • #6
phyzguy said:
I'm sure they exist, but are they nested lists? When you write A[0][0], it means A must be of the form [[something]]. If A is a simple variable (which is what the message implies) or a simple list [], then you will get an error when you try to access A[0][0]. What does the print out of A and B look like?

A and B are nested lists indeed.
They look like A=[[1,2,3],[2,3,4],[2,2,3]] for instance.
But what amazes me is that when I try to work with A[0][0] in the python shell, then there is absolutely no problem whereas with my program (with IDLE) an error is raised...
 
  • #7
jhae2.718 said:
Try adding
Code:
print(type(A))
print(type(B))
before that call to see what type is getting passed. From the error, A or B are integers.

I get type list
 
  • #8
Edit: Your problem is that when [itex]n[/itex] is 1, A and B are no longer nested lists.

Then, the subscripting fails because A[0][0] doesn't exist when e.g. A = [1]
 
Last edited:
  • #9
jhae2.718 said:
Edit: Your problem is that when [itex]n[/itex] is 1, A and B are no longer nested lists.

Then, the subscripting fails because A[0][0] doesn't exist when e.g. A = [1]

sorry for the delay I had no internet access for a while
thank you for your help jhae2.718, it looks like this was the problem indeed!
 

1. What is an "other error" on Python?

An "other error" on Python refers to any type of error that is not specifically categorized as a syntax error, runtime error, or logical error. These errors can include import errors, indentation errors, and type errors, among others.

2. How can I fix an "other error" on Python?

The best way to fix an "other error" on Python is to carefully read the error message and understand what caused the error. Then, you can use resources such as the official Python documentation or online forums to troubleshoot and resolve the issue. It may also be helpful to review your code for potential typos or mistakes.

3. Why do I keep getting an "other error" on Python?

There could be a variety of reasons why you are getting an "other error" on Python. Some common reasons include using incorrect syntax, calling a function with the wrong number of arguments, or attempting to perform an operation on incompatible data types. It is important to carefully review your code and identify the source of the error.

4. Can I prevent "other errors" from occurring on Python?

While it is impossible to completely prevent "other errors" from occurring on Python, there are steps you can take to minimize their frequency. These include writing clean and organized code, using consistent coding conventions, and regularly testing and debugging your code.

5. Are "other errors" considered serious on Python?

The seriousness of an "other error" on Python depends on the context and purpose of your code. In some cases, these errors may be minor and easily fixable, while in other cases they may cause your program to crash or produce incorrect results. It is important to address these errors as soon as possible to ensure the proper functioning of your code.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
1
Views
907
  • Programming and Computer Science
Replies
7
Views
439
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
8K
  • Programming and Computer Science
Replies
2
Views
905
Replies
3
Views
758
Back
Top