次のコードは、値エラーをトリガーします。
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
しかし、値は私のプログラムで3回、関数内で2回グローバル化することで明確に定義されています。私はおそらくいくつかの主要なキーポイントを見落としていました-申し訳ありません。