Issue
I feel dum cuz I was trying to make a simple loop for a matrix to show differents solves and I couldn't fix the index of the array: (FOA I am using the Jupyter Notebook with SageMath 9.3)
A=random_matrix(ZZ,4,4)
k=srange(2,7)
show(k)
i=0
for i in k:
show(A^k[i])
show(k[i])
And I receive that:
[2,3,4,5,6]
"The matrix"
4
"The matrix"
5
"The matrix"
6
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-7-c50cd3e70a78> in <module>
4 i=Integer(0)
5 for i in k:
----> 6 show(A**k[i])
7 show(k[i])
IndexError: list index out of range
How can I print in the right order the k esponent 2,3,4,5,6?
Solution
If you add a show(i)
call, you'll see the value of i
each time is:
2
3
4
5
6
So k[i]
is getting the third element of k
, then the fourth, etc. That's what you're seeing when you show(k[i])
. There's only five elements in the list, so when i=5
, you'll get an IndexError
as you've seen.
Instead, just use i
:
A=random_matrix(ZZ,4,4)
k=srange(2,7)
show(k)
i=0
for i in k:
show(A**i)
show(i)
Answered By - RoadieRich
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.