문제

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 loads TheRules module into local namesapce, not its contents (varaibles, functions, ...).

Use from TheRules import ruleTable to load ruleTable to local namespace.

Or access the variable using TheRules.ruleTable

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top