python - 3 图灵机脚本,在未定义的变量上出错,即使我分配了它

问题描述 投票:0回答:2

我在 python 中的图灵机上工作,我收到一个未定义变量的错误,即使我已经分配了它。有帮助吗?

确切的错误是

Traceback (most recent call last):
  File "main.py", line 71, in <module>
    read()
  File "main.py", line 20, in read
    if state == 0:
UnboundLocalError: local variable 'state' referenced before assignment

我的密码是:

#Turing Machine In Python
#by adrian wheeler

#the script

index = 5
state = 0
tape = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]




def MoveRight():
    index += 1

def MoveLeft():
    index -= 1

def read():
    if state == 0:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 2
            MoveRight()
            read()
    
    if state == 2:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 3
            MoveRight
            read()
    
    if state == 3:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 4
            MoveLeft()
            read()
            
    if state == 4:
        if tape[index] == 1:
            tape.insert(index, "0")
            state = 4
            MoveLeft
            read()
            
    if state == 4:
        if tape[index] == 0:
            tape.insert(index, "0")
            state = 5
            MoveRight()
            read()
            
    if state == 5:
        if tape[index] == 0:
            tape.insert(index, "0")
            state = 5
            MoveRight()
            read()
            
    if state == 5:
        if tape[index] == 1:
            tape.insert(index, "1")
            state = 0
            MoveRight()
            read()
    
#running the script

read()
            

我非常确定我在使用它之前定义了变量。

我试过将它移动到不同的地方,并在谷歌上搜索了一个修复程序,我似乎仍然找不到修复程序。我在学校时正在使用 IDE,也许这就是问题所在,请帮忙?

python python-3.x terminal ide turing-machines
2个回答
0
投票

在函数顶部声明

state
index
tape
为局部变量

每个函数都是一个新的命名空间。这意味着即使变量

state
是在外部 scope 中定义的,它也没有在函数中定义。为了解决这个问题,我们可以使用 python 的
global
关键字
,以确保您的变量在函数中定义。

global
关键字的快速解释:

令人惊讶的是,这段代码包含一个错误:

foo = 1

def do_something():
    foo = 2 # Uh, oh... foo is undefined

让我们修复它:

foo = 1

def do_something():
    global foo # make sure foo is global
    foo = 2 # This works fine!

所以,回到您的代码:我们需要将全局关键字放在所有函数的顶部,如:

#Turing Machine In Python
#by adrian wheeler

#the script

index = 5
state = 0
tape = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]




def MoveRight():
    global index # Make sure our vars are global!
    index += 1

def MoveLeft():
    global index # Make sure our vars are global!
    index -= 1

def read():
    global state, index, tape # Make sure our vars are global!
    if state == 0:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 2
            MoveRight()
            read()
    
    if state == 2:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 3
            MoveRight
            read()
    
    if state == 3:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 4
            MoveLeft()
            read()
            
    if state == 4:
        if tape[index] == 1:
            tape.insert(index, "0")
            state = 4
            MoveLeft
            read()
            
    if state == 4:
        if tape[index] == 0:
            tape.insert(index, "0")
            state = 5
            MoveRight()
            read()
            
    if state == 5:
        if tape[index] == 0:
            tape.insert(index, "0")
            state = 5
            MoveRight()
            read()
            
    if state == 5:
        if tape[index] == 1:
            tape.insert(index, "1")
            state = 0
            MoveRight()
            read()
    
#running the script

read()
       

0
投票

这个错误的原因是默认情况下函数不能访问在它们之外定义的变量。

有两种方法可以解决这个问题。

首先是通过在函数开头键入

global state
来声明您正在引用全局变量状态。这将告诉程序使用全局状态变量,而不是尝试访问它所知道的不存在的变量。

def MoveRight():
    global index
    index += 1

def MoveLeft():
    global index
    index -= 1

def read():

    global state
    global tape
    global index

    if state == 0:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 2
            MoveRight()
            read()
    
    if state == 2:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 3
            MoveRight()
            read()
    
    if state == 3:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 4
            MoveLeft()
            read()
            
    if state == 4:
        if tape[index] == 1:
            tape.insert(index, "0")
            state = 4
            MoveLeft
            read()
            
    if state == 4:
        if tape[index] == 0:
            tape.insert(index, "0")
            state = 5
            MoveRight()
            read()
            
    if state == 5:
        if tape[index] == 0:
            tape.insert(index, "0")
            state = 5
            MoveRight()
            read()
            
    if state == 5:
        if tape[index] == 1:
            tape.insert(index, "1")
            state = 0
            MoveRight()
            read()

虽然第一种方法可行,但首选方法是将状态作为参数传递给函数。这样做的好处是它正在制作一个副本,所以如果它被编辑,它不会在全球范围内改变它。

def MoveRight(index):
    index += 1

def MoveLeft(index):
    index -= 1

def read(state, tape, index):

    if state == 0:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 2
            MoveRight(index)
            read(state, tape, index)
    
    if state == 2:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 3
            MoveRight(index)
            read(state, tape, index)
    
    if state == 3:
        if tape[index] == 0:
            tape.insert(index, "1")
            state = 4
            MoveLeft(index)
            read(state, tape, index)
            
    if state == 4:
        if tape[index] == 1:
            tape.insert(index, "0")
            state = 4
            MoveLeft(index)
            read(state, tape, index)
            
    if state == 4:
        if tape[index] == 0:
            tape.insert(index, "0")
            state = 5
            MoveRight(index)
            read(state, tape, index)
            
    if state == 5:
        if tape[index] == 0:
            tape.insert(index, "0")
            state = 5
            MoveRight(index)
            read(state, tape, index)
            
    if state == 5:
        if tape[index] == 1:
            tape.insert(index, "1")
            state = 0
            MoveRight(index)
            read(state, tape, index)

read(state, tape, index)
© www.soinside.com 2019 - 2024. All rights reserved.