0

I want to save the incoming data into an array that remember its previous array position test.py

global data, in_data
data_hold = {}
data = 0

def start_input(atm_data):
   data_hold[data] = atm_data

When calling twice from other module:

test.start_input (5)
test.start_input (6)

The error output is:

UnboundLocalError: local variable 'data' referenced before assignment

I tried to put data = 0 inside the start_input(atm_data) module, but the data is input into

data_hold[0] = 5
data_hold[0] = 6

I want the output to be:

data_hold[0] = 5
data_hold[1] = 6, and so on
AlexVogel
  • 10,601
  • 10
  • 61
  • 71
anshar ryan
  • 15
  • 1
  • 7

2 Answers2

0

Something like:

data_hold = []

def start_input(atm_data):
    data_hold.append(atm_data)

Should work, without any global stuff.

But that being said, a module isn't a great replacement for a proper class, which is what it appears you want.

jedwards
  • 29,432
  • 3
  • 65
  • 92
0
data = 0
data_hold = {}
atm_data  = {}

def start_input(atm_data):
    global data
    data_hold[data] = atm_data
    data +=1

 testing:
 start_input(5)
 start_input(6)
 start_input(7)

 output:
 data_hold[0] = 5
 data_hold[1] = 6
 data_hold[2] = 7
anshar ryan
  • 15
  • 1
  • 7