[Solved] AttributeError:'unicode' object has no attribute 'append'

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

[Solved] AttributeError:'unicode' object has no attribute 'append'

#1 Post by renardjap »

I'm training myself to make some easy inventories but I have a problem with my list.

inventory file

Code: Select all

init python:
    class Inventory:
        def __init__(self):
            self.items = []

        def add_item(self, item):
            self.items.append(item)
            self.items = ", ".join(self.items)
script file

Code: Select all

label start:
    $ inventory = Inventory()
    $ inventory.add_item("dog")
    $ inventory.add_item("cat")
    "Things on the list: [inventory.items]"

    return
When I execute my code, the script fail and return me an AttributeError:'unicode' object has no attribute 'append'
But when I delete the line self.items = ", ".join(self.items) from the inventory class and I put the line $ inventory.items = ", ".join(inventory.items) in the script file it works !!! I don't understand why because for me it looks the same... How can I fix it ?


Thanks :D
Last edited by renardjap on Sat Feb 29, 2020 4:21 am, edited 1 time in total.

User avatar
xavimat
Eileen-Class Veteran
Posts: 1460
Joined: Sat Feb 25, 2012 8:45 pm
Completed: Yeshua, Jesus Life, Cops&Robbers
Projects: Fear&Love, unknown
Organization: Pilgrim Creations
Github: xavi-mat
itch: pilgrimcreations
Location: Spain
Contact:

Re: AttributeError:'unicode' object has no attribute 'append'

#2 Post by xavimat »

The first call to add_item adds the item and then converts the self.item attribute into a string (that's the "join" part). So the second call tries to add an item to a string.
Try using two different attributes.

Code: Select all

init python:
    class Inventory:
        def __init__(self):
            self.items = []
            self.string = ""

        def add_item(self, item):
            self.items.append(item)
            self.string = ", ".join(self.items)
Comunidad Ren'Py en español: ¡Únete a nuestro Discord!
Rhaier Kingdom A Ren'Py Multiplayer Adventure Visual Novel.
Cops&Robbers A two-player experiment | Fear&Love Why can't we say I love you?
Honest Critique (Avatar made with Chibi Maker by ~gen8)

User avatar
renardjap
Regular
Posts: 75
Joined: Sun Aug 05, 2018 1:08 pm
Location: France ! Cocorico
Contact:

Re: AttributeError:'unicode' object has no attribute 'append'

#3 Post by renardjap »

I didn't know that the .join() turned a list into a string... Yes, it's logical that doesn't work...
I made what you said and it's work perfectly now ! Thanks

Post Reply

Who is online

Users browsing this forum: No registered users