-6

I would like to append a list of string in python as below:

messages = ["hello","hi"]

Expected output

messages = ["hello","hi","bye"]

Her is an example where i do have an error :

"str object has no attribute "append" "

messages = ["really","hey"]
user = ["0.0.0.0"]
serv.bind(('0.0.0.0',12800))
msg, addr = serv.recvfrom(1024)
msg = msg.decode()
user.append(addr)
messages.append(msg)

Full code :

chat = ['Bienvenu dans le chat de Dungeons !','Welcome on Dungeon\'s chat']
user = ["really","hey"]
pseudo = ["foo","this"]
isConnect = [0]



serv = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
user_tab = 0

serv.bind(('0.0.0.0',12800))



while(1):

    msg, addr = serv.recvfrom(1024)
    msg = msg.decode()

    if (msg.find("[pseudo]")!=-1):
        firstPlace = msg.find("[pseudo]")
        secondPlace = msg.find("[pswd]")
        if firstPlace != -1 and secondPlace != -1:
            pseu = msg[firstPlace+9:secondPlace]
            pswd = msg[secondPlace+7:]
            print("New client with infos ip ",addr, ",pseudo", pseu, "and password", pswd)
            user.append(addr)
            pseudo.append(pseu)
            isConnect.append("1")
            send_connect="connect"
            serv.sendto(send_connect.encode(),addr)
BenG
  • 14,826
  • 5
  • 45
  • 60
AnonBird
  • 570
  • 13
  • 27

2 Answers2

1
>>> l = ["hello","hi"]
>>> l.append("bye")
>>> l
['hello', 'hi', 'bye']
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Hi, when i do stuff like that, i have an error : "str object has not attribute append" – AnonBird Jul 16 '15 at 23:03
  • 9
    No you don't, I just showed you that it works. If you are writing code that is producing errors, post a question that shows your **actual code** and the **full error message** that you are getting. – Cory Kramer Jul 16 '15 at 23:04
0

I found the solution, but i don't really understand it. Here is what I changed :

pseu = str(msg[firstPlace+9:secondPlace])
AnonBird
  • 570
  • 13
  • 27