如何在类中使用字典?显示未定义字典

0 投票
1 回答
712 浏览
提问于 2025-04-18 14:33

我确实定义了一个字典,但它一直提示字典“ROOMS”没有定义。

起初我以为是因为这个字典在类外面,但当我把它放到类里面时,还是显示同样的信息。

然后我想知道在类里面怎么引用一个字典?

感谢第一个回答,我已经试过用self.ROOMS,这样可以用。但是如果我有很多字典,是不是都得放在init方法里呢?不过在《Learn Python the Hard Way》里建议我们最好不要在init里面放太多东西。

代码在这里附上,和问题无关的部分已经简化了。

from sys import exit
from random import randint


class Game(object):


    def __init__ (self, start):
        self.quips = ['You died.you kinda suck at this.', 'Nice job, you died...jackass.',
            "Such a luser.",
            'I have a small puppy that\'s bettr at this.']
        self.start = start

    def play (self):
        next = self.start

        while True:
            print "\n--------"
            room = ROOMS[next]
            next = room()

    def death(self):

        print self.quips[randint(0,len(quips)-1)]
        exit(1)


    def central_corridor(self):

        action = raw_input(">")

        if action == "shoot!":

            return 'death'

        elif action == "dodge!":

            return 'death'

        elif action == "tell a joke":

            return 'laser_weapon_armory'

        else :
            print "DOES NOT COMPUTE!"
            return 'central_corridor'



    def laser_weapon_armory(self):

        code = "%d%d%d" %(randint(1,9), randint(1,9), randint(1,9))
        guess = raw_input("[keypad]>")
        guesses = 0

        while guess != code and guesses < 10:
            print "BZZZZEDDD!"
            guesses += 1 
            guess = raw_input("[keypad]>")

        if guess == code:

            return 'the_bridge'
        else:

            return 'death'

    def the_bridge(self):

        action = raw_input("> ")

        if action == "throw the bomb":

            return 'death'

        elif action == "slowly place the bomb":

            return 'escape_pod'
        else:   
            print "DOES NOT COMPUTE!"
            return "the_bridge"

    def escape_pod(self):


        good_pod = randint(1,5)
        guess = raw_input("[pod#]> ")


        if int(guess) != good_pod:

            return 'death'

        else:

            exit(0)


    ROOMS = {
              'death' : death,
              'central_corridor' : central_corridor,
              'laser_weapon_armory' : laser_weapon_armory,
              'the_bridge' : the_bridge,
              'escape_pod' : escape_pod
      }


a_game = Game("central_corridor")
a_game.play()

1 个回答

0

你可以把ROOMS的定义放到你类的__init__方法里面,这样你就可以在后面用self.ROOMS来引用它。同时,也可以在函数名字前加上self....

class Game(object):


    def __init__ (self, start):
        self.quips = ['You died.you kinda suck at this.', 'Nice job, you died...jackass.',
            "Such a luser.",
            'I have a small puppy that\'s bettr at this.']
        self.start = start


        self.ROOMS = {
            'death' : self.death,
            'central_corridor' : self.central_corridor,
            'laser_weapon_armory' : self.laser_weapon_armory,
            'the_bridge' : self.the_bridge,
            'escape_pod' : self.escape_pod
        }

    def play (self):
        next = self.start

        while True:
            print "\n--------"
            room = self.ROOMS[next]
            next = room()

撰写回答