I am designing a program that simulates a Turing machine, with the rules in a separate file for easy editing. Unfortunately, it throws up a NameError when it first tries to compute from the ruleTable.
The offending snippet:
import TheRules
def turIt():
global ruleTable #Global has been used here.
tapeSegment = tape[tapePos]
for x in range(0,len(ruleTable)): #Error here.
if ruleTable[x][2] == machineState and ruleTable[x][3] == tape[tapePos]:
machineState = ruleTable[x][4]
tape[tapePos] = ruleTable[x][5]
move(ruleTable[x][6])
TheRules:
ruleTable = [1]
ruleTable[0] = ("startRule","anyVal","anyVal","1","1",1)
#New rules go down here:
To be precise: "NameError: Global name 'ruleTable' is not defined"
What would be the easiest way to deal with this? I am thoroughly confused by it.
import TheRules
only loadsTheRules
module into local namesapce, not its contents (varaibles, functions, ...).Use
from TheRules import ruleTable
to loadruleTable
to local namespace.Or access the variable using
TheRules.ruleTable