2

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
1

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Just did. Unfortunately I'm a new user so StackOverflow doesn't display my upvotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.