“python – 3 图灵机脚本,出现未定义变量的错误,尽管我已经赋值给它”

huangapple go评论72阅读模式
英文:

python - 3 Turing Machine script, getting errors on an undefined variable, even though I assigned it

问题

#图灵机在Python中的代码部分

#脚本

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():
    global state  # 在此处添加此行以引用全局变量
    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()

#运行脚本

read()
英文:

I'm working on a Turing machine in python and i'm getting an error for an undefined variable even though I have assigned it. Any help?

the exact error is

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

my code is:

#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()
			

I'm very sure I defined the variable before using it.

I've tried moving it around to different spots, and searched up a fix on google, I still can't seem to find the fix. I am using an IDE as i'm at school, maybe that's the problem, please help?

答案1

得分: 0

以下是您要翻译的内容:

默认情况下,导致此错误的原因是函数无法访问在它们之外定义的变量。

有两种解决方法。

第一种方法是在函数开头键入global state,以声明您正在引用全局变量state。这将告诉程序使用全局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)
英文:

The reason for this error is that by default functions can't access variables that are defined outside them.

There are 2 ways to solve this.

The first is to just declare that you are referencing the global variable state by typing global state at the beginning of the function. This will tell the program to use the global state variable instead of trying to access a variable that for as it knows, does not exist.

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()

Although the first method works, the preferred way is to pass the state as an argument into the function. The good part about this is that it is being made a copy, so if it's edited it won't change it on a global scale.

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)

答案2

得分: 0

在你的函数顶部将stateindextape声明为局部变量:

每个函数都是一个新的命名空间。这意味着即使变量state在外部作用域中定义了,它在函数中也未定义。为了解决这个问题,我们可以使用Python的global关键字,确保在函数内部定义了这些变量。

global关键字的简要解释:

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

foo = 1

def do_something():
    foo = 2 # 哎呀... foo 未定义

让我们来修复它:

foo = 1

def do_something():
    global foo # 确保 foo 是全局的
    foo = 2 # 这个工作正常!

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

# 在Python中的图灵机
# 由Adrian Wheeler编写

# 脚本

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

def MoveRight():
    global index # 确保我们的变量是全局的!
    index += 1

def MoveLeft():
    global index # 确保我们的变量是全局的!
    index -= 1

def read():
    global state, index, tape # 确保我们的变量是全局的!
    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()

# 运行脚本

read()
英文:

Declare state, index and tape as local variables at the top of your functions

Every function is a new namespace. This means that even though the variable state was defined in the outer scope, it is not defined in the function. To fix this we can use python's global keyword, to ensure that your variables are defined within the function.

Quick explanation of global keyword:

Surprisingly, this code contains an error:

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

Let's fix it:

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

So, back to your code: We need to put global key words at the top of all your functions as in:

#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()

huangapple
  • 本文由 发表于 2023年5月17日 22:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76273443.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定