-1

I get this error whenever I try to run my code. I believe both the lists's summary_name and summary lists are not working.

Error:

  File "C:/Users/Asus/Google Drive/Sun Gleam & Fine Works/System Improvement/Accounting/Voucher Reports/Voucher Reports", line 12, in <module>
    summary_name[i] = data_listofaccounts['Account Name']
NameError: name 'summary_name' is not defined

Code:

import pandas as pd
import matplotlib.pyplot as plt

entries_csv = "C:\\Users\\Asus\\Desktop\\Entries.csv"
listofaccounts_csv = "C:\\Users\\Asus\\Desktop\\List of Accounts.csv"

data_entries = pd.read_csv(entries_csv)
data_listofaccounts = pd.read_csv(listofaccounts_csv)

i = 0
for account_name in data_listofaccounts['Account Name']:
    summary_name[i] = data_listofaccounts['Account Name']
    for debit_account in data_entries['DEBIT ACCOUNT']:
        if account_name == debit_account:
            summary[i] += data_entries['DEBIT AMOUNT']
    i += 1

for p in range(i):
    print(summary[p])
    print(summary_name[p])
Pherdindy
  • 1,168
  • 7
  • 23
  • 52

1 Answers1

1

This

summary_name[i]

assumes that there already is something that has the name summary_name and that allows access using [i]. Since Python is a dynamic language, this can be anything. All that is necessary to use [i] on an object is to implement object.__getitem__ for reading and object.__setitem__ for writing. How is Python supposed to know which of the many classes that do this you want summary_name to be an instance of?

So you first have to define summary_name, for example as an empty list

summary_name = ["I", "am", "a", "list"]

a tuple

summary_name = ("I", "am", "a", "tuple")

or a string

summary_name = "I am a string."

All these allow read access using summary_name[i].

Since you want to use write access, you must define an object that allows summary_name[i] = something. A list with 100 entries, all initialized to None could be created using

summary_name = [None] * 100

Only after this can you set entries of this list.