+ 1

How to append an object to a list at the time of instantiation?

https://code.sololearn.com/clY8WJ2VtMkz/?ref=app I want to define method in the Game_Object class that upon instantiation will add the name of the instance which in my program is Fred to the dictionary Objects? I'm unsure however on how to accomplish this

27th Jul 2017, 1:24 PM
StealthyTaco
StealthyTaco - avatar
5 Answers
+ 1
objects = {} def __init__(self, name): self.name = name Game_Object.objects[self.name] = self Is this what you're wanting to do? Your object variable is a list, so I'm a bit unsure if this is what you're asking for. If not, someone will lecture me. lol
27th Jul 2017, 1:46 PM
Sapphire
0
@ Sapphire your answer is what I wanted unfortunately its throwing me a type error saying list indices must be an int or slice I'm unsure why this is due to the fact I can make a list with string elements in it? do you know why this is? I added my new code below https://code.sololearn.com/clY8WJ2VtMkz/?ref=app
28th Jul 2017, 1:09 PM
StealthyTaco
StealthyTaco - avatar
0
@Robert Hi again Robert, so I had another look at your code. I'm having difficulty trying to understand what your intention is. So perhaps you can explain to me what it is you're trying to accomplish in the program? The reason I ask for this is because I'm not getting the error you're receiving, I am instead getting NameError with your Commands on Line 29. This is a different issue, but I can also help with that too. When calling for the "Commands" dictionary, you have to use the "Game_Object.Commands" in order to access it, since it belongs to that class.
28th Jul 2017, 2:40 PM
Sapphire
0
@ Sapphire what I am trying to do is have the instance I'm creating added to a list which your previous reply did do however when I left Objects as an empty list it threw an error saying it had to be an int or slice object so I changed Objects to an empty dictionary but now I have the error saying string Fred has no attribute desc. so what I want to accomplish is to add the instance name to a list or dictionary at the time of instantiation I need the instance name to retain its assigned attributes then when my get input function gets the input "examine fred" it should return the description of the instance. I apologize for being difficult and appreciate all your help! https://code.sololearn.com/clY8WJ2VtMkz/?ref=app
28th Jul 2017, 3:57 PM
StealthyTaco
StealthyTaco - avatar
0
@ Robert Hi again, I see what you're now trying to do. I decided to modify and give you an example of what you were trying to do in your code. I will delete it in a week or something. I removed the input part because you're trying to do too many things without understanding some of the other issues going on - in other words ... it makes it less confusing. You can add the input part from the user later. The important thing is to understand how objects work in Python. The script is similar to yours, just removed the Get_Input() and replaced it with get_desc() - just so you can understand it better. I also added several comments at different points to help you understand what is happening when, and where. I also added an internal examine to the Game_Object, just to show you that as well. Finally, there are some call examples at the bottom of the script. Also, I don't mind helping you on this, it's not a problem. I'm just slow to respond sometimes. Let me know if this helps, or if I'm just blind to the issue. lol https://code.sololearn.com/chsE2v03G1ft
28th Jul 2017, 6:13 PM
Sapphire