Need help with create_spend_chart

Hi folks,

I have advanced a bit on my budget app project and now I’m working on the create_spend_chart ( ) function. Right now this is the input I’m currently having:

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

So far so good. Now I have to add the categories’ names vertically under the dashes. However… How ?

I am thinking that each line should be a list with a margin (with five ’ ’ spaces) followed by first letter, spaces, first letter of the second name… and so on.

The thing is I don’t know how long each category name will be, so I can’t say in advance how many of these lines I’m gonna have: How do I find the length of the longest word in the list? And after this, how can I can create n number of variables?

My code (just the function) so far:

def create_spend_chart(category):

    zero_line = '  0|'
    separator = '  '
    dashes = '-'
    white_space = ' '
    margin = '    '
    lower_margin= '     '
    zero_line_dashes = margin+'-'
    ten_line = ' 10|'
    twenty_line= ' 20|'
    thirty_line= ' 30|'
    forty_line= ' 40|'
    fifty_line = ' 50|'
    sixty_line = ' 60|'
    seventy_line = ' 70|'
    eighty_line = ' 80|'
    ninety_line = ' 90|'
    hundred_line = '100|'
    title = 'Percentage spent by category'
    lista = [zero_line,ten_line,twenty_line,thirty_line,forty_line,fifty_line,sixty_line,seventy_line,eighty_line,ninety_line,hundred_line]    
    index = lista.index(zero_line)

    for items in category:
        withdrawal = sum(items.withdrawals)
        deposits = sum(items.deposits)
        percentage_spent = (withdrawal * 100) / deposits

        how_many_os = percentage_spent / 10  
        for i in range(index, int(how_many_os)):
            lista[i] = lista[i]+' o'        

    lista.reverse()
    display = '\n'.join(lista)
    length_category = len(category)
#___Adjusting the dashes size
    for i in range(length_category):
        zero_line_dashes = margin+'-' *((length_category*2)+2)
    output = title+'\n'+display+'\n'+zero_line_dashes

#___Now for the 'fun' part. I stopped here
    for words in category:
        i = 0     
     
    return output

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