Home > Net >  How to convert a python function that has print methods from for loops into a return value?
How to convert a python function that has print methods from for loops into a return value?

Time:01-04

I am a neophyte in Python and in programming in general and I am still learning from freeCodeCamp and currently taking a certification evaluation from them.

In regards to the title, this is what I've got so far:

def create_spend_chart(categories):
    withdrawals_name = []
    withdrawals_sum = 0
    for category in categories:
        withdrawals_sum  = category.widthdrawal_tracker
        withdrawals_name.append([category.name, category.widthdrawal_tracker])

    for withdrawal in withdrawals_name:
        withdrawal.append(int((withdrawal[1]/withdrawals_sum) * 10) * 10)

    vals_to_be_evaluated = [a[2] for a in withdrawals_name]

    print('Percentage spent by category')

    for row in range(100, -10, -10):
        if row < 100 and row > 0:
            print(f" {row}|", ' '.join(
                ['o' if digit >= row else ' ' for digit in vals_to_be_evaluated]))
        elif row == 0:
            print(f"  {row}|", ' '.join(
                ['o' if digit >= row else ' ' for digit in vals_to_be_evaluated]))
        else:
            print(f"{row}|", ' '.join(
                ['o' if digit >= row else ' ' for digit in vals_to_be_evaluated]))
    print(' '*4   '-'*(2*len(vals_to_be_evaluated) 1)   '-')

    str_val_eval = [a[0] for a in withdrawals_name]
    str_val = []
    spl_str = []

    for str_val_indv in str_val_eval:
        str_val.append(len(str_val_indv))
        spl_str.append([str_val_indv])

    max_str_val = max(str_val)

    for x in range(max_str_val):
        print(' '*5, end='')
        for y in spl_str:
            try:
                print(y[0][x], end=' ')
            except IndexError:
                print(' ', end=' ')
        print()

I am tasked in creating a function that takes a list of objects as arguments that will return a string illustration of the bar chart based on the objects' withdrawal values. Based on the function I've written above, this is its output:

create_spend_chart([income_food, income_entertainment, income_business])
# actual output:
Percentage spent by category
100|      
 90|      
 80|      
 70| o    
 60| o    
 50| o    
 40| o    
 30| o    
 20| o o  
 10| o o
  0| o o o
    --------
     F E B
     o n u
     o t s
     d e i
       r n
       t e
       a s
       i s
       n
       m
       e
       n
       t

But based from the exam, the function should be invoked like this:

print(create_spend_chart([income_food, income_entertainment, income_business]))

And if I run the line above, this is its output instead:

Percentage spent by category
100|      
 90|      
 80|      
 70| o    
 60| o    
 50| o    
 40| o    
 30| o    
 20| o o  
 10| o o
  0| o o o
    --------
     F E B
     o n u
     o t s
     d e i
       r n
       t e
       a s
       i s
       n
       m
       e
       n
       t
None

What should I change so that my function can have a return value instead?

CodePudding user response:

I would like to add to @Andrew's answer. Yes, you should store the intermediate strings and return them from your function so that they could be printed by the caller.

However, it's much more efficient to create a list and add the strings to it. Then use ".join(iterable)" to convert the list to a single string like so:

return "\n".join(strings)

It's simple. Add the separator you want inside the double quotes and pass the list to join as an argument.

  • Related