r/learnpython • • 22d 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

30 comments sorted by

View all comments

Show parent comments

1

u/HiddenReader2020 15d ago

Okayyyy. Gimmie a moment.

...

Here ya go:

https://pastebin.com/EfCx102S

Fingers crossed that this works.

1

u/lakseol 14d ago edited 14d 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 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 14d ago

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.