Budget App- Concatenating instead of reassignment?

Tell us what’s happening:
The code below is returning a TypeError saying that I am trying to concatenate a string and a float. When I run type() all pieces are floats. I am trying to reassign the Total variable to the float of each iterable added to the variable. Where is the bug?


TypeError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_11272\283273581.py in <cell line: 1>()
----> 1 print(Cats.str())

~\AppData\Local\Temp\ipykernel_11272\3942022756.py in str(self)
34 else:
35 cap = str(v);
—> 36 Total += (minus * float(cap));
37 if len(cap) > 7:
38 capn = cap[0:7] + “\n”

TypeError: can only concatenate str (not “float”) to str

Also, I am running the code in Jupyter-Lab since replit fails, wanting me to have Create_Spend_Chart done.

def str(self):
stars = int((30 - len(self.name))/2)
Line1 = stars*‘’ + str(self.name) + stars’*’

    #Make Middle of printout plus initiate Total
    middle = ''
    Total = 0
    for item in Cats.ledger:
        for k,v in item.items():
            if 'deposit' in str(v):
                minus = ''
            elif'Transfer from' in str(v):
                minus = 1.0
                minus = -1.0
            if k == 'description':
                if len(str(v)) <= 23:
                    swag = str(v)
                    swag = str(v)[0:23]
            elif k == 'amount':
                if str(v)[-2] == ".":
                    cap = str(v) + "0";
                    Total += (minus * float(cap));
                    cap = str(v);
                    Total += (minus * float(cap));
                if len(cap) > 7:
                    capn = cap[0:7] + "\n"
                    capn = cap + "\n"
        space = 30-len(cap)-len(swag)
        middle = middle + swag + space*" " + capn
        #Finalize Total variable
        if str(Total)[-2] == ".":
            Total = "Total: " + str(Total) + '0'
            Total = "Total: " + str(Total)
    return Line1 + '\n' + middle + Total

Your browser information:

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

Challenge: Scientific Computing with Python Projects - Budget App

Link to the challenge:

Can you explain what the str function does? I don’t get an error although I had to fix this line:

  Line1 = stars*'' + str(self.name) + stars'*'

I have solved it. There was a discrepency in the block of code I was working with and how it returned as just the block. It needed to be printed because the return only wokred with the parts up to and including that block of code.

Thanks for your help!

P.S. That code was for the first line of text which took the stars variable which qas quantified to wqual half of the stars needed for the line and it concatenates it to the category and the other half of the stars.

1 Like

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