Scientific Computing with Python Projects - Budget App

I don’t know what is the problem I’ve tried everything I can imagine, it is shown as supposed and doing correct calculations, It’s the last thing left to get my certification, please help.

Failed:create_spend_chart should print a different chart representation. Check that all spacing is exact.

Your code so far

class Category:
    def __init__(self, name):
        self.name = name
        self.ledger = []
    def deposit(self, amount, description=""):
        self.ledger.append({"amount": amount, "description": description})
    def withdraw(self, amount, description=""):
        if self.check_funds(amount):
            self.ledger.append({"amount": -amount, "description": description})
            return True
        else:
            return False
    def get_balance(self):
        return sum(item["amount"] for item in self.ledger)
    def transfer(self, amount, category):
        if self.check_funds(amount):
            self.withdraw(amount, f"Transfer to {category.name}")
            category.deposit(amount, f"Transfer from {self.name}")
            return True
        else:
            return False
    def check_funds(self, amount):
        return amount <= self.get_balance()
    def __str__(self):
        title = f"{self.name:*^30}\n"
        items = ""
        total = 0
        for item in self.ledger:
            items += f"{item['description'][:23]:23}" + f"{item['amount']:7.2f}" + '\n'
            total += item['amount']
        return title + items + "Total: " + str(total)

# SPEND CHART
#import math
def create_spend_chart(categories):
    withdrawals = {category.name: sum(item['amount'] for item in category.ledger if item['amount'] < 0) for category in categories}
    total_withdrawals = sum(withdrawals.values())
    
    percentages = {category.name: int((withdrawals[category.name] / total_withdrawals) * 10) * 10 for category in categories}
    
    #percentages = {category.name: math.floor((withdrawals[category.name] / total_withdrawals) * 10) * 10 + 10 if (withdrawals[category.name] / total_withdrawals) % 1 >= 0.5 else math.floor((withdrawals[category.name] / total_withdrawals) * 10) * 10 for category in categories}
    
    chart = "Percentage spent by category\n"
    for i in range(100, -1, -10):
        chart += f"{i:3}| "
        for category in categories:
            if percentages[category.name] >= i:
                chart += "o  "
            else:
                chart += "   "
        chart += "\n"

    chart += "    " + "-" * (len(categories) * 3 + 1) + "\n"

    max_length = max(len(category.name) for category in categories)
    for i in range(max_length):
        chart += "     "
        for category in categories:
            if i < len(category.name):
                chart += category.name[i] + "  "
            else:
                chart += "   "
        chart += "\n"

    return chart



#EXAMPLE
import random
food = Category("Food")
food.deposit(random.randint(100, 1000), "initial deposit")
food.withdraw(random.randint(10, 100), "groceries")
food.withdraw(random.randint(10, 100), "restaurant and more food for dessert")

clothing = Category("Clothing")
clothing.deposit(random.randint(100, 1000), "initial deposit")
clothing.withdraw(random.randint(10, 100), "shirts")
clothing.withdraw(random.randint(10, 100), "pants")

auto = Category("Auto")
auto.deposit(random.randint(100, 1000), "initial deposit")
auto.withdraw(random.randint(10, 100), "gas")
auto.withdraw(random.randint(10, 100), "maintenance")

print(create_spend_chart([food, clothing, auto]))

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36

Challenge Information:

Scientific Computing with Python Projects - Budget App

What is the spend chart you are generating?

Percentage spent by category
100|
90|
80|
70|
60|
50|
40|
30| o o
20| o o o
10| o o o
0| o o o

----------
 F  C  A  
 o  l  u  
 o  o  t  
 d  t  o  
    h     
    i     
    n     
    g

looks like this in the preview

Capture d’écran 2024-03-04 à 23.10.30

Your output:

Percentage spent by category\n100|          \n 90|          \n 80|          \n 70|    o     \n 60|    o     \n 50|    o     \n 40|    o     \n 30|    o     \n 20|    o  o  \n 10|    o  o  \n  0| o  o  o  \n    ----------\n     B  F  E  \n     u  o  n  \n     s  o  t  \n     i  d  e  \n     n     r  \n     e     t  \n     s     a  \n     s     i  \n           n  \n           m  \n           e  \n           n  \n           t  \n

expected output:

Percentage spent by category\n100|          \n 90|          \n 80|          \n 70|    o     \n 60|    o     \n 50|    o     \n 40|    o     \n 30|    o     \n 20|    o  o  \n 10|    o  o  \n  0| o  o  o  \n    ----------\n     B  F  E  \n     u  o  n  \n     s  o  t  \n     i  d  e  \n     n     r  \n     e     t  \n     s     a  \n     s     i  \n           n  \n           m  \n           e  \n           n  \n           t  

The difference occurs at the very end of the output.

TBH it’s pretty impossible to determine this given the information from the test. I had to dig up the test info on github.

I added this line just before the return in the create spend chart to examine the raw string output in your code:

print("test",repr(chart))
1 Like

Indeed I would never have guessed it, thx so much!

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