I am a beginner working my way through How To Think Like A Computer Scientist. I am having a problem with an exercise that multiplies a matrix by a scalar. def scalar_mult(n, m), n being the scalar and m being the matrix.
My code works but I guess its not a pure function because if I put in
>>> m = [[1, 2], [3, 4]]
>>> scalar_mult(3, m)
my return is [[3, 6], [9, 12]] which is correct, but when i check m after I run my function I get [[3, 6], [9, 12]] instead of [[1, 2], [3, 4]]. I don't understand this because I cloned m and used mx throughout the function. This is the first time I have posted on a forum so please excuse me if my format is incorrect.
[code=python]
def scalar_mult(n, m):
mx = m[:]
(my code is here)
[/code]
My code works but I guess its not a pure function because if I put in
>>> m = [[1, 2], [3, 4]]
>>> scalar_mult(3, m)
my return is [[3, 6], [9, 12]] which is correct, but when i check m after I run my function I get [[3, 6], [9, 12]] instead of [[1, 2], [3, 4]]. I don't understand this because I cloned m and used mx throughout the function. This is the first time I have posted on a forum so please excuse me if my format is incorrect.
[code=python]
def scalar_mult(n, m):
mx = m[:]
(my code is here)
[/code]
Comment