0

So I need my output to look like this: output

The final question will be: How do I go about adding the values of each column to the rowstring?

I'm defining a function that formats how the lines will print, it looks like this:

def line_string(row_title, row_data, data_type):

    row_title = f"{row_title:38}"
    row_string = row_title   

    columns = years[-1]

    for column in range(columns+1):

        # Concatenate row_string per the data_type function arugment as per below
        # Make each of the data columns 9 characters wide

        # If data_type equals "decimal", then format as decimal with the specified width

        if data_type == "integer":
            row_string = row_string + f"{row_data[column]:9,}"

        elif data_type == "decimal":
            row_string = row_string + f"{row_data[column]:9,.2f}"

        elif data_type == "percent":
            row_string = row_string + f"{row_data[column]:9,.2%}"

    return row_string 

# this is a test:

line_string("Cumulative sum", cumulatitve_sum, "decimal") 

I get this error though:


TypeError                                 Traceback (most recent call last)
 in 
     44     return row_string
     45 
---> 46 line_string("Cumulative sum", cumulatitve_sum, "decimal")

 in line_string(row_title, row_data, data_type)

     35         elif data_type == "decimal":
---> 36             row_string = row_string + f"{row_data[column]:9,.2f}"
     37 

TypeError: 'function' object is not subscriptable

How do I go about adding the values of each column to the rowstring?

doelleri
  • 19,232
  • 5
  • 61
  • 65
  • 5
    "Subscripting" means accessing with `[]`, so the error refers to `row_data[column]`. What is the value of `cumulatitve_sum` (misspelled btw) that you're passing as `row_data`? – Thomas Mar 17 '20 at 16:22
  • Perhaps you mean even just `line_string("Cumulative sum", cumulatitve_sum(), "decimal")` – kojiro Mar 17 '20 at 16:26
  • Maybe you meant `f"{row_data(column):9,.2f}` ? `cumulatitve_sum` is a function so it can accept parameters only with `()`, not with `[]`. – sanitizedUser Mar 17 '20 at 16:37
  • Does this answer your question? [TypeError: 'function' object is not subscriptable - Python](https://stackoverflow.com/questions/29101836/typeerror-function-object-is-not-subscriptable-python) – AMC Mar 17 '20 at 16:54
  • Please do not share information as images unless absolutely necessary. See: https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors, https://idownvotedbecau.se/imageofcode, https://idownvotedbecau.se/imageofanexception/. – AMC Mar 17 '20 at 16:54

1 Answers1

0

I'm extremely dumb and I did a typo.

"Perhaps you mean even just line_string("Cumulative sum", cumulatitve_sum(), "decimal") – kojiro 1 hour ago"

Yes, that is exactly right.