0

I'm trying to interpret data I am planning to send betweeen machines with a chosen target where I will send the data to and the data the following code is supposed to do this(the target is a number of any length and has to start with a "/") I know the whole code isn't great and I am probably using wrong names for pretty much everything but I hope with a bit of help it will work

def Interpret(command):
    if(command[0] != "/"):
        return "ERROR"
    o = 1
    targetstr = []
    while(command[o] != " "):
        targetstr.append[command[o]]
        o = o + 1
    try:
        "".join(targetstr)
        target = int(targetstr)
    except:
        return "ERROR"
    data = []
    for i in range(o + 1, len(command)):
        data.append(command[i])
    return [target, "".join(data)]

everytime I run the code I get this error message: targetstr.append[command[o]] TypeError: 'builtin_function_or_method' object has no attribute 'getitem' (pbviously it is supposed to give me an array with the target and the data)

2
  • 1
    Voting to close as a typo since append is correctly called in one of the two occurrences. Commented Aug 3, 2019 at 15:35
  • "".join(targetstr) doesn't do anything useful; it leaves targetstr unchanged while ignoring the return value of join. Did you mean targetstr = "".join(targetstr)? (Also, targets = [] would be a better name to use for the list of targets; it's not a string yet.) Commented Aug 3, 2019 at 15:36

1 Answer 1

4

list.append() is a function

targetstr.append[command[o]]

Should be

targetstr.append(command[o])

The reason why you are getting that particular error is that when you use the square bracket syntax foo[bar] python is actually calling foo.__getitem__(bar) and the append method does not have the attribute/method __getitem__

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

Comments

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.