Budget app Python

can anyone tell me how to return such a line in str_(self) function inside a class. printing such a sequence is not a problem but how to return it
please help
this is the sequence.The values in this sequence are stored in a list so how to retrieve them in a single return statement
Food
initial deposit 1000.00
groceries -10.15
restaurant and more foo -15.89
Transfer to Clothing -50.00
Total: 923.96
**

**My code
class Category:

def __init__(self,name):
    self.name=name
    self.ledger=list()
def check_funds(self,amount):
    fund=0
    n=len(self.ledger)
    for i in range(n):
        fund=fund+self.ledger[i]["amount"]
    if fund<amount:
        return False
    else:
        return True
def deposit(self,amount,description=""):
    #initialising a dictionary
    self.dep=dict()
    #adding the amount and description to dictionary
    self.dep["amount"]=amount
    self.dep["description"]=description
    #adding the deposit to ledger list
    self.ledger.append(self.dep)

def withdraw(self,amount,description=""):
    #checking if total amount less than or greaten than amount to be withdrawn
    l=self.check_funds(amount)

    if(l==True):
        self.withd=dict()
        self.withd["amount"]=-(amount)
        self.withd["description"]=description
        self.ledger.append(self.withd)
        return True
    else:
        return False
def get_balance(self):
    fund=0
    n=len(food.ledger)
    #retrieving the total fund in ledger
    for i in range(n):
        fund=fund+food.ledger[i]["amount"]
    return fund
def transfer(self,amount,obname):
    objectname=obname.name
    a=self.withdraw(amount,f"Transfer to {objectname}")
    b=obname.deposit(amount,f"Transfer from {self.name}")
    if(a==True):
        return True
    else:
        return False
def check_funds(self,amount):
    fund=0
    n=len(self.ledger)
    for i in range(n):
        fund=fund+self.ledger[i]["amount"]
    if fund<amount:
        return False
    else:
        return True
def __str__(self):

**

Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Budget App

Link to the challenge:

1 Like
def __str__(self):
        title = f"{self.name:*^30}\n"
        items = ""
        total = 0
        for i in range(len(self.ledger)):
            items += f"{self.ledger[i]['description'][0:23]:23}" + \ # break line
            f"{self.ledger[i]['amount']:>7.2f}" + '\n'
            total += self.ledger[i]['amount']

        output = title + items + "Total: " + str(total)
        return output
2 Likes

thank you.I was stuck at that portion and your solution really helped me

1 Like

Hi,
can you please explain the last thing — :23 in below code line…?
items += f"{self.ledger[i][‘description’][0:23]:23}

And in the below code what does :>7.2f does?
f"{self.ledger[i][‘amount’]:>7.2f}

thanks…!

It is confusing…do you have an alternate way

Hi,
it’s just text formatting
https://docs.python.org/3/library/string.html#format-specification-mini-language
in this case with f-string
http://zetcode.com/python/fstring/

‘23’ it is width

‘>’ is right-aligned
‘7’ is width
‘.2’ is precision
‘f’ is type (float)

alternate way
items += f"{self.ledger[i]['description'][0:23]:23}{self.ledger[i]['amount']:>7.2f}\n"

5 Likes

i dont understand teh code can u put all of it in one place