【问题标题】:How to use dictionary in class? it shows the dict is not defined [closed]如何在课堂上使用字典?它显示字典未定义[关闭]
【发布时间】:2014-07-24 01:44:04
【问题描述】:

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

一开始我以为是因为dict在课堂外,但是当我把它放在课堂上时,它仍然显示相同的信息。

那我想知道如何在课堂上查阅字典? 感谢第一个答案,我已经尝试过 self.ROOMS 并且它有效。但是,如果我有很多 dicts,我是否必须将它们全部放在方法 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()

【问题讨论】:

  • 如果您将ROOMS 字典移到顶部,就在您的init 方法下方并调用它self.rooms,会发生什么?
  • @akonsu self.ROOMS 不起作用,因为它不是实例变量,OP 需要改为 Game.ROOMS
  • 我确实尝试为这个问题 OP 找出合适的答案,但答案是 There are too many errors in your code. Would you like to try again? Y/N
  • @Lego Stormtroopr 我从“硬性学习 python”复制而来,如果不使用字典,它在我的电脑上运行良好。那么,您提到的错误类型是什么?

标签: python class dictionary undefined


【解决方案1】:

您可以将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()

【讨论】:

  • 如果我需要很多字典怎么办?我应该把它们都放在 init 方法中吗?还有其他方法可以引用类中的字典吗??
猜你喜欢
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多