Scientific Computing with Python Projects - Budget App

Tell us what’s happening:
Hello world! I have a AssertionError at line 102. I tried for some time and can’t find my mistake maybe im doing it wrong. Time flies by and I dont know what to do… need help on how to do the right spacing. Sorry for my bad english I try x)

Your code so far
Here is my code:

def create_spend_chart(categories):
  # Calculate the total amount spent
  total_spent = sum(category.get_spent() for category in categories)

  # Calculate the percentage spent
  percentages = [(category.get_spent() / total_spent) * 100 for category in categories]
  
  # Build the spend chart
  chart = "Percentage spent by category\n"
  for i in range(100, -10, -10):
    chart += f"{i:3d}| "
    for percentage in percentages:
      if percentage >= i:
        chart += "o  "
      else:
        chart += "   "
    chart += "\n"

  # Add the horizontal line
  chart += "    " + "-" * (3 * len(categories) + 1) + "\n"

  # Determine the maximum category name length
  max_name_length = max(len(category.category_name) for category in categories)

  # Write the category names vertically below the bars
  for i in range(max_name_length):
    chart += "     "
    for category in categories:
      if i < len(category.category_name):
        chart += category.category_name[i] + "  "
      else:
        chart += "   "
    chart += "\n"

  return chart

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Scientific Computing with Python Projects - Budget App

Link to the challenge:

Missing the catagory class here.

Can you share your full code or replit?

Yes here is the full code:

"""
Represents the category class
"""
class Category:
  """
  Initializes a Category object
  Parameters:
  - category_name (str): the name of the category
  """
  def __init__(self, category_name):
    self.category_name = category_name
    self.ledger = []

  """
  Deposits an amount into the category ledger
  Parameters:
  - amount (float): the amount deposited
  - description (str): description of the deposit
  """
  def deposit(self, amount, description=""):
    self.ledger.append({"amount": amount, "description": description})

  def check_funds(self, amount):
    total = sum(item["amount"] for item in self.ledger)
    return total >= amount

  def withdraw(self, amount, description=""):
    if self.check_funds(amount):
      self.ledger.append({"amount": -amount, "description": description})
      return True
    else:
      return False

  def transfer(self, amount, category):
    if self.check_funds(amount):
      self.withdraw(amount, f"Transfer to {category.category_name}")
      category.deposit(amount, f"Transfer from {self.category_name}")
      return True
    else:
      return False

  def get_balance(self):
    balance = 0
    for item in self.ledger:
      balance += item["amount"]
    return balance

  def get_spent(self):
    spent = 0
    for item in self.ledger:
      if item["amount"] < 0:
        spent -= item["amount"]
    return spent

  def __str__(self):
    title = f"{self.category_name}".center(30, "*") + "\n"
    items = ""
    total = 0
    for item in self.ledger:
      description = item["description"][:23].ljust(23)
      amount = "{:.2f}".format(item["amount"]).rjust(7)
      items += f"{description}{amount}\n"
      total += item["amount"]
    total_line = f"Total: {total:.2f}"
    return title + items + total_line


def create_spend_chart(categories):
  # Calculate the total amount spent
  total_spent = sum(category.get_spent() for category in categories)

  # Calculate the percentage spent
  percentages = [(category.get_spent() / total_spent) * 100 for category in categories]
  
  # Build the spend chart
  chart = "Percentage spent by category\n"
  for i in range(100, -10, -10):
    chart += f"{i:3d}| "
    for percentage in percentages:
      if percentage >= i:
        chart += "o  "
      else:
        chart += "   "
    chart += "\n"

  # Add the horizontal line
  chart += "    " + "-" * (3 * len(categories) + 1) + "\n"

  # Determine the maximum category name length
  max_name_length = max(len(category.category_name) for category in categories)

  # Write the category names vertically below the bars
  for i in range(max_name_length):
    chart += "     "
    for category in categories:
      if i < len(category.category_name):
        chart += category.category_name[i] + "  "
      else:
        chart += "   "
    chart += "\n"

  return chart

Your Output:
‘Perc[364 chars] m \n e \n n \n t \n’
Expected:
'Perc[364 chars] m \n e \n n \n t ’

You have an extra \newline at the very end of your output, which might mean you need to add the newline at the beginning of a string, instead of the end, so the last string just ends without \newline (or however else you want to get rid of it)

1 Like

Thank you it solved my problem. Everything is done ^^

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.