Budget exercise

Hey people!

Actually there isn’t any error (as far as I can tell). It is just that it took me sometime to define the create_spend_chart for the budget exercise, and I finally got it.

But I think there should be a more understandable/short/fast way to do it. I just wasn’t able to come with it.

If you can share the way you did it, that would help me understand and maybe find a better way to solve these kinds of problems.

Thank you, and have a great day!

def create_spend_chart(categories):
text = ["100| ", " 90| ", " 80| ", " 70| ", " 60| ", " 50| ", " 40| ", " 30| ", " 20| ", " 10| ", " 0| "]
d = dict()
total = float()
for item in categories:
y = re.findall(’*([a-zA-Z]+)’, str(item))

    for line in str(item).split("\n"):
        if re.search('^Transfer', line) or re.search('-([0-9.]+)', line) is None:
            continue

        x = re.findall('-([0-9.]+)', line)
        total = total + float(x[0])

        try:
            d[y[0]] = d[y[0]] + float(x[0])
        except:
            d[y[0]] = float(x[0])

for value in d.values():
    z = 10
    w = 9
    percentage = (value / total) * 10
    if percentage == z:
        text[0] = text[0] + "o  "
        pass
    else:
        while not w < percentage < z:
            z = z - 1
            w = w - 1

    while w > -1:
        text[10 - w] = text[10 - w] + "o  "
        w = w - 1
    while z < 11:
        text[10 - z] = text[10 - z] + "   "
        z = z + 1        

text.append("    " + "-" * (len(text[10]) - 4))

for key in d.keys():
    i = 12
    for c in key:
        try:
            text[i] = text[i] + c + "  "

        except:
            text.append(" " * 5 + c + "  ")

        if (i - 1) == 11:
            pass
        elif len(text[i]) < len(text[i - 1]):
            text[i] = text[i].rjust(len(text[i - 1]), " ")
        i = i + 1

    if len(text[-1]) < len(text[i-1]):
        for idx in range(len(text)):
            if len(text[idx]) < len(text[12]):
                text[idx] = text[idx].ljust(len(text[12]), " ")

for idx in range(len(text)):
    if len(text[idx]) < len(text[11]):
        text[idx] = text[idx].ljust(len(text[11]), " ")

t = "Percentage spent by category\n" + "\n".join(text)
return t

Your browser information:

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

Challenge: Budget App

Link to the challenge:

hey 7King,

could you fix the formatting issue in your post?

It would also be helpful if you add brief comments in your code (you can add comments with the ‘#’ symbol in Python) that explain what a certain code passage does.

(You should only add comments to passages of your code that are difficult to understand.)

So it is a bit easier to understand your intentions in your code.

On the first look I would suggest you split the “create_spend_chart” function into several, smaller functions. That is always a good start when you refactor (structurally improve) your code.

best
Dennis

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