Python 2.7 无值错误时的值错误
以下代码触发值错误:Python 2.7 无值错误时的值错误,python-2.7,undefined,defined,Python 2.7,Undefined,Defined,以下代码触发值错误: from pygame import * init() from random import * t = True f = False inventory = [] class block(object): def __init__(self,break_time,break_content,color): self.break_time = break_time self.break_content = break_content
from pygame import *
init()
from random import *
t = True
f = False
inventory = []
class block(object):
def __init__(self,break_time,break_content,color):
self.break_time = break_time
self.break_content = break_content
self.color = color
def brek(self):
inventory.append(self.break_content)
end = block(1-0,None,(0,0,0))
gem = block(10,"agem",(0,0,100))
stone = block(3,"cobble",(100,100,100))
iron = block(5,"iron_ore",(25,50,100))
dirt = block(2,"dirt",(139,69,19))
locations = [a for a in range (60)]
unervise = {}
def generate(x_axis):
global unevise
global locations
for a in range(60):
global unevise
global locations
if a == 0 or a == 1:
unevise[(locations[a],x_axis)] = end
if a in range(2,35):
if a in range(2,10) and randint(1,10) == 1:
unevise[(locations[a],x_axis)] = gem
elif a in range(2,30) and randint(1,5) == 1:
unevise[(locations[a],x_axis)] = iron
else:
unevise[(locations[a],x_axis)] = stone
if a in range(35,40):
unevise[(locations[a],x_axis)] = dirt
以下是错误:
Traceback (most recent call last):
File "C:/Users/Ben/Documents/Python Files/My first comp openable.py", line 46, in <module>
generate(a)
File "C:/Users/Ben/Documents/Python Files/My first comp openable.py", line 28, in generate
unevise[(locations[a],x_axis)] = end
NameError: global name 'unevise' is not defined
回溯(最近一次呼叫最后一次):
文件“C:/Users/Ben/Documents/Python Files/My first comp openable.py”,第46行,在
生成(a)
文件“C:/Users/Ben/Documents/Python Files/My first comp openable.py”,第28行,在generate中
未调整[(位置[a],x_轴)]=结束
NameError:未定义全局名称“unevise”
但是这个值是明确定义的,在我的程序中是三次,在函数中是两次全局化。
我可能忽略了一些主要的关键点-抱歉。您定义的是
取消设置,而不是取消设置:
unervise = {}
另外,不需要多个全局
声明。您拼错了变量的名称:它被称为unervise
,而不是unevise
。原因很多,因为它一直有错误,所以我添加了更多。