问题
The following code triggers a value error:
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
Here's the error:
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
But the value is clearly defined, thrice in my program, twice globaling it inside the function. I probably have overlooked some major key point- sorry.
回答1:
You define unervise
, not unevise
:
unervise = {}
Also, there's no need for the multiple global
declarations.
来源:https://stackoverflow.com/questions/16924723/value-error-when-no-value-error