Python Forum

Full Version: Need to find a mistake in my code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A=Matrix([[1,2,0],[4,5,0],[7,8,9]]);A
def Kostenmatrix(A):
    C=Matrix(A.nrows(),[])
    for i in range(0.A.nrows()):
        for j in range(0,A.nrows()):
            if i==j:
                C[i,j]=0
            elif A[i,j]==0 and i<>j:
                C[i,j]=10^4
            else:
                C[i,j]=A[i,j]

    return C
Kostenmatrix(A)
︡1d7ac8e1-aea5-4323-8e3c-45ee125883e2︡{"stdout":"[1 2 0]\n[4 5 0]\n[7 8 9]\n"}︡{"stderr":"Error in lines 13-13\nTraceback (most recent call last):\n  File \"/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py\", line 1013, in execute\n    exec compile(block+'\\n', '', 'single') in namespace, locals\n  File \"\", line 1, in <module>\n  File \"\", line 3, in Kostenmatrix\n  File \"sage/structure/element.pyx\", line 484, in sage.structure.element.Element.__getattr__ (build/cythonized/sage/structure/element.c:4377)\n    return self.getattr_from_category(name)\n  File \"sage/structure/element.pyx\", line 497, in sage.structure.element.Element.getattr_from_category (build/cythonized/sage/structure/element.c:4486)\n    return getattr_from_other_class(self, cls, name)\n  File \"sage/cpython/getattr.pyx\", line 254, in sage.cpython.getattr.getattr_from_other_class (build/cythonized/sage/cpython/getattr.c:1901)\n    raise dummy_attribute_error\nAttributeError: 'sage.rings.integer.Integer' object has no attribute 'A'\n"}︡{"done":true}︡
Iam using " sage" which is pretty similar to python. I just hope that someone can help me becasue i simply cannot understand why my function Kostenmatrix(A) is putting out an error.
I've never used sage before, but take a look at this link:

https://ask.sagemath.org/question/8131/a...ibute-mod/

seems sage doesn't work properly (sometimes) with range, due the Sage Integer type.

It's just a guess, as I've never used sage before.
At line 4, there is a dot . between 0 and A. It should be a comma ,.
thanks, now everything works.