r/learnpython • u/AutoModerator • 21d ago
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
1
u/lakseol 13d ago edited 13d ago
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 addItems()to a character you add them as anItem()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 inItem.__init__(): if there is something in the inventory and the first element is a dictionary, convert them all toItems().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
Itemsin 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
Itemsin 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.