0

Below is my code, when I print(a_list), it shows nothing. I don't understand why a_list is still empty when I call the function get_graph.

a_list = []

def get_graph():
    files = os.listdir(path)
    for file in files:
        df = pd.read_csv(file)
        for value in values:
            if True:
                a_list.append(file)
get_graph()
NAM
  • 9
  • 2
  • 6
    Your code is not valid - `values` is not defined anywhere. – Tom Dalton Nov 27 '20 at 15:45
  • Don't forget to declare `global a_list` within the function – dspr Nov 27 '20 at 15:48
  • Please read [mre].. – wwii Nov 27 '20 at 15:48
  • Yeah, and also, you cannot change the value of global variable like this... – AgentNirmites Nov 27 '20 at 15:49
  • 2
    @dspr it's not necessary to `global a_list` since the value of `a_list` isn't being changed, `.append()` is just calling a function on `a_list` which is ok – Macattack Nov 27 '20 at 15:49
  • 1
    @dspr Should not be necessary. `a_list` is never assigned within the function. – user2390182 Nov 27 '20 at 15:49
  • 1
    @dspr : [Python Scopes and Namespaces](https://docs.python.org/3/tutorial/classes.html#python-scopes-and-namespaces), [Structure of a program-Naming and binding](https://docs.python.org/3/reference/executionmodel.html#structure-of-a-program). – wwii Nov 27 '20 at 15:51
  • You're right, sorry for the noise. – dspr Nov 27 '20 at 15:52
  • If you are using an IDE **now** is a good time to learn its debugging features - like stepping through execution, setting breakpoints, and examining values. Or you could spend a little time and get familiar with the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Also, printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Nov 27 '20 at 15:53
  • Thanks for your comment. But I still don't understand how I can change values of global variable(in this case, using a function, I want to append values into global variable). – NAM Nov 28 '20 at 00:25

0 Answers0