r/learnpython • • Sep 21 '26

Ask Anything Monday - Weekly Thread

Welcome to another /r/learnPython weekly "Ask Anything* Monday" thread

Here you can ask all the questions that you wanted to ask but didn't feel like making a new thread.

* It's primarily intended for simple questions but as long as it's about python it's allowed.

If you have any suggestions or questions about this thread use the message the moderators button in the sidebar.

Rules:

  • Don't downvote stuff - instead explain what's wrong with the comment, if it's against the rules "report" it and it will be dealt with.
  • Don't post stuff that doesn't have absolutely anything to do with python.
  • Don't make fun of someone for not knowing something, insult anyone etc - this will result in an immediate ban.

That's it.

1 Upvotes

30 comments sorted by

View all comments

Show parent comment

1

u/lakseol Sep 28 '26

Again you have posted incorrect code. Bad indentation at lines 68 and 120. You have to get this right. Anybody trying to help you doesn't want to have to guess how to indent your code and hope they get it right. If your problem is adding code to the lousy reddit interface remember that I mentioned pastebin.com where you can post code, get a page showing your code and then post a link to that page here, like this:

https://pastebin.com/uYR3zqqR


the computer says that init takes 2 arguments, but 3 were given. Umm...I only see two. I don't know where the third one is

This is a little confusing. The confusion starts when you use an instance method. They have the self parameter as the first parameter and your calling code doesn't explicitly pass self, it's automatically added by python. When you get that "expected 2 but 3 given" python is counting the self, so the three parameters are "self", "name" and "stats_dict". Keyword parameters are counted separately. When you call the function (line 139):

new_warrior = RPG_Character('John', warrior_class)

you pass the data dictionary as a positional parameter, so it's counted as an extra positional parameter. "self" + "name" + stats_dict" == 3. If you check the example I showed you in my last comment you will see that my call to the constructor was:

t1 = Test(**alice)   # notice the "**"

The two asterisks in **alice says to unpack the dictionary alice and pass the contents as keyword parameters like key=value. That means the __init__() method sees keyword parameters "name"="Alice", "age"=42 when it executes. In the example the __init__() method started:

def __init__(self, **kwargs):

The **kwargs parameter is shorthand for python to collect all undefined keyword parameters into a dictionary which the example iterated over. It's not a positional parameter. You need the ** both when calling the method and defining it. The parameter name kwargs is usually used in python, meaning "keyword args".


You have another problem in that __init__() definition. You pass the data dictionary into the method as well as the name parameter. That works fine when initially creating the objects and then saving to JSON. But what happens when you read the dictionary from JSON and try to create the character. Where does name come from?

1

u/HiddenReader2020 Sep 28 '26

First of all, ack, I didn’t see the first “indent problem” comment from before.  I’ll try to remember it going forward.

Second, though, I will say that that comment is a touch outdated, as I’ve already made an updated version, but had to put it in another comment .  Now, just as a heads up, this isn’t in a pastebin, so apologies if it still has some intention errors.

1

u/lakseol Sep 28 '26

I stopped counting after guessing how to fix 4 indentation errors. Please fix your code.

After you post your code please copy it back into a file and execute it to check for errors.

1

u/HiddenReader2020 Sep 28 '26

Okayyyy. Gimmie a moment.

...

Here ya go:

https://pastebin.com/EfCx102S

Fingers crossed that this works.

1

u/lakseol Sep 29 '26 edited Sep 29 '26

OK, i can read the pastebin. And immediately I see a reason why you might have had trouble posting code correctly before. You have TAB characters in your code. This is something that python programmers generally don't do. I have my environment set up to insert four space characters when I press the TAB key. Up to you, but most programmers stay away from TABs due to the problems they cause.


After all this time I can't really remember what your latest problem was, but I think it was about handling Items in the inventory.

You handle that the same way you handled the basic characters: modify the Item() constructor to take a **dictionary. The extra little problem in doing that is when you add Items() to a character you add them as an Item() instance (of course) but when restoring from a JSON file the inventory list is a list of dictionaries. I got around that by adding a small test in Item.__init__(): if there is something in the inventory and the first element is a dictionary, convert them all to Items().


Here is my code based on yours:

https://pastebin.com/XidMEU9j

One change I made that isn't mentioned above was to handle printing the state of a character differently. When creating the __str__() return string I made it much more compact, and included the player's inventory. It's a bit more work but I found it a lot easier to look at and check that changes made were correct. I also added small prints to explain what was done at each stage of creating the characters, which is a hint on what to check. I found it helpful, maybe you will.


This code a little messy. I will just say again that actually storing python references to Items in an inventory adds a lot of work. I've written a small example text adventure and I found it cleaner to handle items in the player's inventory as an ID, with a dictionary to convert the ID to the actual item reference. Same for places in the map and monsters when you get to that.

You may not have thought that far ahead yet, but you will also need to store Items in places in the map. And monsters, if you have them, have their own inventory. Monsters can steal from the player or pick up stuff lying around, for instance.

1

u/HiddenReader2020 Sep 29 '26

I see. I've set my editor (Geany) to make the indent type to be spaces, but the problem is that I have to do that *per file*, so that definitely won't get annoying immediately!

Anyway, I will say that the reason I made the items objects was to make things easier and less messy as opposed to using dictionaries all the time. And what do you mean handling items as an ID, and not as an actual dictionary or an object, assuming those two things are different?

I'll probably look through your code and see what you've done differently and hopefully learn from it, but uhhh...no promises I'll be able to fully fulfill that.

EDIT: Oh, and I haven't used any comprehensions, like list of dictionary comprehensions, simply because I don't find them particularly readable. I prefer to just type out the full procedures in those cases.

1

u/lakseol Sep 30 '26

the problem is that I have to do that per file

That doesn't sound very useful. Make sure there isn't a way to make that change permanent. Or change to a decent editor/IDE.

what do you mean handling items as an ID, and not as an actual dictionary or an object

Currently you create the inventory as a list of references to Item objects, doing something like:

new_item = Item(...)
new_rogue.add_item(new_item)

That's easy to understand, but doing that means you complicate saving data to a JSON file because JSON doesn't handle references to python objects and you have to extract all the attributes of the item and save them as a JSON dictionary. When reading the JSON data back into python objects you have to create the Item object from the dictionary.

The "ID" approach doesn't store python objects in the list. Every Item object has a unique "ID" string, and it's that ID string that is saved in the inventory list. This makes the save/restore process simpler. When creating the items you add an entry in a dictionary that maps the ID string to a reference to the object, like this:

class Item:                                                                 
    def __init__(self, id, description):                                    
        self.id = id                                                        
        self.description = description                                      
                                                                            
id2obj = {}                                                                 
inventory = []                                                              

item1 = Item("axe1", "An Elven axe")                                        
id2obj["axe1"] = item1                                                      
inventory.append(item1.id)                                                  

item2 = Item("book2", "A very old book with a cracked red cover")           
id2obj["book2"] = item2                                                     
inventory.append(item2.id)                                                  

print(f"{inventory=}")                                                      
                                                                            
# list what's in the inventory                                              
for id in inventory:                                                        
    print(id2obj[id].description)

This allows you to list the inventory by iterating over the ID strings, getting the item object from the dictionary using the ID string as a key, then printing the item description. When restoring the objects from the JSON save file you create that dictionary automatically. Sounds complicated but it simplifies the save/restore process a lot. I use it for everything: places, monsters and items.

1

u/HiddenReader2020 Sep 30 '26

Hmmm....you say all that, but I'm still not sure how this would work out when "restoring the objects from the JSON file", not to mention having to include other stats like buy/sell price, any variable amounts that need to be adjusted, like some might have a modifier for stats like defense, others heal health/mana, etc. I understand that you're just saving strings to a list, but then how are you using those strings to get the items back if there's no reference to them or something? Like, I'm looking at this code, and it seems like the items have to be made at a point where you can't access them if you find a file to load from, if that makes any sense.

I dunno. I should probably mess with this code to fully understand it, but I'm not convinced just yet.

1

u/lakseol Sep 30 '26 edited Sep 30 '26

then how are you using those strings to get the items back if there's no reference to them or something?

The code example shows how to do that. You use the dictionary to find the value for the ID string. The value is the reference to the item. That's all happening in this code that prints the description of an item in the inventory:

for id in inventory:
    print(id2obj[id].description)
    #       ^    ^        ^
    #       |    |        +-- description attribute of the item
    #       |    +-- id string lookup in dictionary
    #       +-- dictionary mapping id to reference

So id2obj[id] evaluates to the item object. Print out what is in the id2obj dictionary.

1

u/HiddenReader2020 Sep 30 '26

Ah. ...? So what I meant was that if I did the whole if-else test for whether the file exists, I would have to define the items before that if-else test, right? Or maybe something else? I'm just worried that when I get the id strings from the JSON files, they'll be meaningless, since in my head, what items they refer to hasn't been defined yet.

EDIT: Oh. I see that in my editor, the "id" in the print statement is in a different color. So does that mean that id has a special meaning?

→ More replies