Scientific Computing with Python Projects - Budget App

Tell us what’s happening:
Hello, i have a problem with the test unit, I don’t understand why it return that output.

Your code so far

class Category:
    def __init__(self,name):
        self.name = name
        self.ledger = []
        #list of number
        self.l1 = []
        #list of description
        self.l2 = []
    def deposit(self, amount, description = ""):
        self.amount = amount
        self.l1.append(amount)
        self.l2.append(description)
        if description == "":
            x = '{"amount":'+' '+f'{amount}'+','+' '+'"description"'+':'+' '+f'{description}'+'}'
            self.ledger.append(x)
        else:
            x = '{"amount":'+' '+f'{amount}'+','+' '+'"description"'+':'+' '+f'{description}'+'}'
            self.ledger.append(x)
    def withdraw(self, amount2, description2 = ""):
        if description2 == "":
            x = '{"amount":'+' '+f'{-amount2}'+','+' '+'"description"'+':'+' '+f'{description2}'+'}'
            if float(self.amount) >= float(amount2):
                self.amount = float(self.amount) - float(amount2)
                self.ledger.append(x)
                self.l1.append(-amount2)
                self.l2.append(description2)
                return True
            elif float(self.amount) < float(amount2):
                return False
        else:
            x = '{"amount":'+' '+f'{-amount2}'+','+' '+'"description"'+':'+' '+f'{description2}'+'}'
            if float(self.amount) >= float(amount2):
                self.amount = float(self.amount) - float(amount2)
                self.ledger.append(x)
                self.l1.append(-amount2)
                self.l2.append(description2)
                return False, self.ledger
            elif float(self.amount) < float(amount2):
                return True
    def get_balance(self):
        return float(self.amount)
    def transfer(self, amount4, name):
        if float(amount4) > float(self.amount):
            return True
        else:
            self.withdraw(amount4,f'Transfer to {name.name}')
            name.deposit(amount4, f'Transfer from {name.name}')
            return True, self.amount
    def check_funds(self, amount3):
        if float(amount3) > float(self.amount):
            return False
        else:
            return True
    def __str__(self):
        #first line
        fl = '{:*^30}'.format(self.name)
        #last line
        ll = f'Total: {self.amount}'
        ancho = 23
        #segundo parafo
        sp = ""
        for i,j in zip(self.l1 , self.l2):
            if len(str(j)) <= ancho:
                x = f'{str(j):22s}' + " " + str(i).rjust(7)
                sp = sp + x +'\n'
            elif len(str(j)) > ancho:
                x = str(j)[:23] + " " + str(i).rjust(6)
                sp = sp + x +'\n'
            fo = fl + '\n' + sp + ll
        return fo

def create_spend_chart(name1, name2 = "", name3 = "", name4 = ""):
    var = [name1,name2,name3, name4]
    var = [i for i in var if i !=""]
    percentage = [float(format(i.amount*10 / i.l1[0], '.2F')) for i in var]
    intro = f'Percentage spent by category'
    #constrution du premier membre
    line = ""
    for i in range(11):
        line = line + str(10*(10-i)).rjust(3)+'|'
        for y in percentage:
            if y >= 10 - i:
                new = " " + "o"
                line = line + new
            else:
                new = " " + " "
                line = line + new
        line = line + '\n'
    largo = 2*len(var)+1
    tline = ("-"*largo).rjust(4+largo)
    output = intro + '\n'+ line + tline
    varname = [i.name for i in var ]
    return output

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Budget App

Link to the challenge:

Each item in the ledger should be a dict, with amount and descritption keys. Currently ledger contains list of strings.

In the exercice they say that ledger is a list and not a dict.

Ledger is a list. Each item in ledger is dict (object). It’s a list of dicts, not list of strings.

1 Like