Python function call unable to format output

def cal_average(comments,post):
    total_ask_comments = 0
    for row in ask_posts:
        total_ask_comments += int(row[4])  
 
    avg_comments = comments / len(post)
    return avg_comments

print("avg-ask comments:",cal_average(total_ask_comments,ask_posts)) 
print("avg-show comments:",cal_average(total_show_comments,show_posts))


print("avg-ask comments:",'{:,.2f}'.format(cal_average(total_ask_comments,ask_posts))

I want to format the output to two decimal places , but am unable to do so please advise? I have attempted above and searched stackoverflow but found no answers.

With what I have I get this error:

SyntaxError: invalid syntax

for this line

print("avg-ask comments:",'{:,.2f}'.format(cal_average(total_ask_comments,ask_posts))

I would like it formatted as 40.03 instead of 40.0948884 as per output along the lines of these two:

 print("avg-ask comments:",cal_average(total_ask_comments,ask_posts)) 
    print("avg-show comments:",cal_average(total_show_comments,show_posts))

Hi @jamesberentsen

The first thing that catches your eye is the lack of brackets.

Your line at the end of 2 closing brackets

print("avg-ask comments:",'{:,.2f}'.format(cal_average(total_ask_comments,ask_posts))

Must be 3.

print("avg-ask comments:",'{:,.2f}'.format(cal_average(total_ask_comments,ask_posts)))

first bracket closes “cal_average” function, second “format” method, third closes “print”

1 Like

Thanks moriturus7,

I was thinking it might not be possible, but it was actually just missing a closing bracket . It works now.

Many thanks
JB