Value error when no value error

≯℡__Kan透↙ 提交于 2019-12-13 04:29:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!