4

I keep receiving this error when I try to run this code for the line "encoded.append("i")":

AttributeError: 'str' object has no attribute 'append'

I cannot work out why the list won't append with the string. I'm sure the problem is very simple Thank you for your help.

def encode(code, msg):
    '''Encrypts a message, msg, using the substitutions defined in the
    dictionary, code'''
    msg = list(msg)
    encoded = []
    for i in msg:
        if i in code.keys():
            i = code[i]
            encoded.append(i)
        else:
            encoded.append(i)
            encoded = ''.join(encoded)
    return encoded
2
  • 2
    you have not any "decoded.append("i")": in your cod do you mean "encoded.append("i")":?? Commented Jan 10, 2015 at 16:17
  • What is the logic behind encoded = ''.join(encoded) Commented Jan 10, 2015 at 16:21

5 Answers 5

3

You set encoded to string here:

encoded = ''.join(encoded)

And of course it doesn't have attribute 'append'.

Since you're doing it on one of cycle iteration, on next iteration you have str instead of list...

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

Comments

0
>>> encoded =["d","4"]
>>> encoded="".join(encoded)
>>> print (type(encoded))
<class 'str'> #It's not a list anymore, you converted it to string.
>>> encoded =["d","4",4] # 4 here as integer
>>> encoded="".join(encoded)
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    encoded="".join(encoded)
TypeError: sequence item 2: expected str instance, int found
>>> 

As you see, your list is converted to a string in here "".join(encoded). And append is a method of lists, not strings. That's why you got that error. Also as you see if your encoded list has an element as integer, you will see TypeError because, you can't use join method on integers. Better you check your all codes again.

Comments

0

Your string conversion line is under the else clause. Take it out from under the conditional, and the for loop so that it's the last thing done to encoded. As it stands, you are converting to a string halfway through your for loop:

def encode(code, msg):
'''Encrypts a message, msg, using the substitutions defined in the
dictionary, code'''
msg = list(msg)
encoded = []
for i in msg:
    if i in code.keys():
        i = code[i]
        encoded.append(i)
    else:
        encoded.append(i)

# after all appends and outside for loop
encoded = ''.join(encoded)
return encoded

2 Comments

If that encoded list has an integer element it will throw TypeError. It's not a solution for this question.
Nothing was stated about the type of input this function would give. This also won't work with many other types, not simply integers. The question was "why?", not, "what should this be?", thus it's perfectly reasonable, even if it's not ideal.
0

You are getting the error because of the second expression in you else statement.

    ''.join(encoded) returns a string that gets assigned to encoded

Thus encoded is now of type string. In the second loop you have the .append(i) method in either if/else statements which can only be applied to lists and not strings.

Your .join() method should appear after the for loop right before you return it.

I apoligise if the above text does not appear right. This is my first post and I still trying to figure out how this works.

Comments

-1

Obviously you cannot append only works for lists and it will not work for strings. For appending any thing at the end of string you just have to concatenate it with your original string.

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.