Reputation: 35
In the function "enText" changes its value even though I don't modify it.
e = 5 and n = 6. enText is an array of ints. I modify the values in the array after passing it to changeTxt. I then return changeTxt but for some reason enText ends up being modified as well.
No idea why.
#Function for encryption
def encrypt(e, n, enText):
#e=5 & n=6
changeTxt = enText
print(enText)
#prints [18, 15, 2, 9, 14]
for x in range(0, len(changeTxt)):
#problem here!!!!!!!!
tmp = changeTxt[x]**e
tmp = tmp % n
changeTxt[x] = tmp
print(enText)
#prints [0, 3, 2, 3, 2]
return changeTxt
Upvotes: 2
Views: 215
Reputation: 16505
Your line
changeTxt = enText
only copies the reference to the list, but both point to the same list. So changes to changeTxt
will affect enText
as well.
Instead try to clone the list like this:
changeTxt = enText[:]
or
changeTxt = list(enText)
Upvotes: 1