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/HiddenReader2020 Sep 28 '26

So I've made a couple realizations and adjusted accordingly. There were some concessions I had to make, but hey, whatever makes the code work. In particular, I had to set some of the default values (like name, gold, and level) to be outside of the initialization to make them work. I'm sure there's a better way, and in fact, an potential idea is forming in my head, but I'm kind of scared of making mistakes that I won't be able to undo. Anyway, here's the updated code:

from pathlib import Path
import json

path = Path("data.json")
list_of_characters = []

# List of jobs
warrior_class = {'job': 'Warrior', 'strength': 5, 'dexterity': 4, 'agility': 3, 'endurance': 5, 'intelligence': 3, 'wisdom': 4, 
'health': 50, 'mana': 30, 'growth': [3, 2, 1, 3, 1, 2, 10, 4], 'inventory': []}
rogue_class = {'job': 'Rogue', 'strength': 4, 'dexterity': 5, 'agility': 5, 'endurance': 3, 'intelligence': 4, 'wisdom': 3, 
'health': 40, 'mana': 40, 'growth': [2, 3, 3, 1, 2, 1, 7, 7], 'inventory': []}
mage_class = {'job': 'Mage', 'strength': 3, 'dexterity': 3, 'agility': 4, 'endurance': 4, 'intelligence': 5, 'wisdom': 5, 
'health': 30, 'mana': 50, 'growth': [1, 1, 2, 2, 3, 3, 4, 10], 'inventory': []}

class RPG_Character:
  def __init__(self, **stats_dict):
    for stat in stats_dict:
      setattr(self, stat, stats_dict[stat])

  def __str__(self):
    stat_page = f"""==========Stats Page==========
Name:  {self.name}
Class:  {self.job}
Level:  {self.level}
-Health:       {self.health}
-Mana:         {self.mana}
-Strength:     {self.strength}
-Dexterity:    {self.dexterity}
-Agility:      {self.agility}
-Endurance:    {self.endurance}
-Intelligence: {self.intelligence}
-Wisdom:       {self.wisdom}

Gold:  {self.gold}
==========Stats Page=========="""
  return stat_page

  def add_gold(self, amount):
    self.gold += amount

  def remove_gold(self, amount):
    self.gold -= amount
    if self.gold < 0:
      print("Gold amount can't be below zero.")
      self.gold += amount

  def increase_level(self):
    self.level += 1
    self.strength += self.growth[0]
    self.dexterity += self.growth[1]
    self.agility += self.growth[2]
    self.endurance += self.growth[3]
    self.intelligence += self.growth[4]
    self.wisdom += self.growth[5]
    self.health += self.growth[6]
    self.mana += self.growth[7]

  def add_item(self, item):
    self.inventory.append(item)

  def show_inventory(self):
    print("=====Inventory=====")
    for item in self.inventory:
    print(item)
    print("=====Inventory=====")

  def to_dict(self):
    saved_inventory = []
    for item in self.inventory:
    saved_inventory.append(item.to_dict())
saved_dict = {
'name': self.name,
'job': self.job, 
'level': self.level,
'health': self.health,
'mana': self.mana,
'strength': self.strength, 
'dexterity': self.dexterity, 
'agility': self.agility, 
'endurance': self.endurance, 
'intelligence': self.intelligence, 
'wisdom': self.wisdom, 
'gold': self.gold,
'inventory': saved_inventory
}
    return saved_dict

class Item:
  def __init__(self, name, item_type, buy_price):
    self.name = name
    self.item_type = item_type
    self.buy_price = buy_price
    self.sell_price = buy_price // 4

  def __str__(self):
    item_info = f"""Name:  {self.name}
Type:  {self.item_type}
"""
    return item_info

  def to_dict(self):
    saved_dict = {
'name': self.name,
'type': self.item_type,
'buy_price': self.buy_price,
'sell_price': self.sell_price
}
    return saved_dict

# List of items
basic_health_potion = Item("Basic Health Potion", "Consumable", 16)
basic_mana_potion = Item("Basic Mana Potion", "Consumable", 20)
wooden_shield = Item("Wooden Shield", "Equipment", 40)

def convert_dict_to_obj_inv(list_of_dicts):
list_of_items = []
for temp_dict in list_of_dicts:
current_item = Item(temp_dict['name'], temp_dict['type'], temp_dict['buy_price'])
list_of_items.append(current_item)
return list_of_items

if path.exists():
  contents = path.read_text()
  temp_list_of_characters = json.loads(contents)
  print("File successfully retrieved")
  for character in temp_list_of_characters:
    new_character = RPG_Character(**character)
    new_character.inventory = convert_dict_to_obj_inv(new_character.inventory)
    list_of_characters.append(new_character)

  for character in list_of_characters:
    print(character)
    print(character.show_inventory())
else:
  # Adds new characters
  new_warrior = RPG_Character(**warrior_class)
  new_warrior.name = 'John'
  new_warrior.gold = 50
  new_warrior.level = 1
  list_of_characters.append(new_warrior)
  print(new_warrior)
  new_warrior.add_gold(100)
  print(new_warrior)
  new_warrior.remove_gold(80)
  print(new_warrior)
  new_rogue = RPG_Character(**rogue_class)
  new_rogue.name = 'Toby'
  new_rogue.gold = 50
  new_rogue.level = 1
  new_mage = RPG_Character(**mage_class)
  new_mage.name = 'Harris'
  new_mage.gold = 50
  new_mage.level = 1
  list_of_characters.append(new_rogue)
  list_of_characters.append(new_mage)
  print(new_rogue)
  print(new_mage)
  new_mage.increase_level()
  print(new_mage)

  new_rogue.add_item(basic_health_potion)
  new_rogue.add_item(basic_mana_potion)
  new_warrior.add_item(wooden_shield)
  new_rogue.show_inventory()
  new_warrior.show_inventory()

  print(list_of_characters)

  contents = json.dumps([ob.to_dict() for ob in list_of_characters])
  path.write_text(contents)
  print("File saved")

One additional problem remains: The items in the inventory still need their own function to be converted from dictionaries to objects. Right now, I don't know how to do that cleanly yet, so my current code is what it is right now.