1

So I am having trouble with the code in the way that it gives an error: "line 30, in (ciphertextBinary[i])[x] = "0" TypeError: 'str' object does not support item assignment". I have no idea as to why this happens as all that I am doing is changing a subtext of a string and replacing it with a string. If you can come up with any solutions it would be much obliged as I have looked for answers for a while now but have had no success. Before I had this error:

     if (textBinary[i])[x] == (pad[i])[x]:
        (ciphertextBinary[i])[x] = "0"
    elif (textBinary[i])[x] != pad[i][x]:
        (ciphertextBinary[i])[x] = "1"

the code was not working and returned the input so I changed it to what I think would make it work but have come across this error instead.

string = ""
string2 = ""
length = 0
lenght2 = 0
pad = ""


string = input("Enter String To Encrypt: ")
string2 = input("Enter PAD: ")

length = len(string)
length2 = len(string2)

textBinary = list(string)
pad = list(string2)
ciphertextBinary = list(string)
ciphertext = list(string)

for i in range(0, length):
    textBinary[i] = "{0:b}".format(ord(string[i]))
    for x in range(0, len(textBinary[i])):
        ciphertextBinary[i] = ciphertextBinary[i] + "a"

for i in range(0, length2):
    pad[i] = "{0:b}".format(ord(string2[i]))

for i in range(0, length):
    for x in range(0, len(textBinary[i])):
        if (textBinary[i])[x] == (pad[i])[x]:
            (ciphertextBinary[i])[x] = "0"
        elif (textBinary[i])[x] != pad[i][x]:
            (ciphertextBinary[i])[x] = "1"

for i in range(0, length):
    ciphertext[i] = int(ciphertextBinary[i], 2)
    print(chr(ciphertext[i]))
1
  • You can't replace individual characters in a string. You have to replace the whole string. Commented Jun 13, 2016 at 22:49

2 Answers 2

3

Python does not let you change a single character within a string. Strings are considered an immutable type in Python just like numbers or tuples. Thus, your attempt to do string assignment within a string is somewhat like asking to change the third digit in the number 8435 to 0. Rather, you may be able to do something like this:

(ciphertextBinary[i]) = (ciphertextBinary[i])[:x] + "0" + (ciphertextBinary[i])[x+1:]

This works by setting your variable of interest equal to everything up to character x, plus the new character, plus all the characters that were after x.

I hope this helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe mention strings are immutable which causes the inability of assignment
As far as I can see, your solution has fixed my problem but I have some more testing to do with it and hopefully it will work. Thanks.
0

Strings are immutable in python. If ciphertextBinary is short, than the answer by jonathanking works.

If it is too long, you may want to create list first (which is mutable):

ciphertextBinaryL = list(ciphertextBinary)
[your last three loops]
ciphertextBinary = "".join(ciphertextBinaryL)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.