0

I just familarized myself with a Turing machine. I'm in the process of making a semi-accurate virtual Turing machine but I ran into a seemingly simple problem that I just know there is a solution for. I researched online but couldn't find anything that satisfied my issue.

How do I make the variable 'l' callable within the function. It must be callable because if I define the initial value of the variable within the function, when the function loops it will reset the value to 0.

Here is my code:

blanktape = []
for x in range(1,251):
    x = ' '
    blanktape.append(x)

global l
l = 1
non = ' '
head = blanktape[l]
symbols = [3, 'ee', 'x']

def mconfigb():
    if head == non:
        blanktape[blanktape.index(head)] = 0
        l = l + 2

def mconfigc():
    if head == non:
        blanktape[blanktape.index(head)] = 1
        l = l + 2

def turingmachine():
        while l < len(blanktape) + 1:
            mconfigb()
            mconfigc()
        return blanktape

print turingmachine()
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
01110100
  • 793
  • 4
  • 9
  • 17
  • 1
    I'll parrot comments made in the answers below but I agree with them. First, its a perfect opportunity to use a class. Second, one letter variables tend to be very problematic to work with. Don't be afraid to use longer name like head_location or something like that. – Laurent Bourgault-Roy Mar 20 '13 at 00:20

3 Answers3

0
def function(input):
    global globvar
    ....

they have to be declared per-function. but, I think you should wrap all your creation into a turingmachine class and use instance variables.

thkang
  • 11,215
  • 14
  • 67
  • 83
0

You need to put:

global l

within the function itself (and you may want to rethink your variable naming conventions, l is not a good name).

For example:

xyzzy = 1

def fn():
    global xyzzy
    xyzzy = xyzzy + 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0
def mconfigb():
     global l

Other functions do this also.

Yarkee
  • 9,086
  • 5
  • 28
  • 29