以下代码触发值错误:
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
但是值清楚地定义了,在我的程序中三次,在函数内部两次全局化。 我可能忽略了一些重要的关键点 - 抱歉。
答案 0 :(得分:0)
您定义unervise
,而不是unevise
:
unervise = {}
此外,不需要多个global
声明。