学习Python很困难,ex43

  • 本文关键字:ex43 Python 学习 python
  • 更新时间 :
  • 英文 :


很抱歉这个问题已经被问过很多次了。我看过很多这样的书,带着确切的问题,看到了他们的答案,但他们要么不适合我,要么对我简单的头脑来说太术语太重了。这是我的代码;

from sys import exit
from random import randint
class Scene(object):
    def enter(self):
        print("Scene not configured")
        exit(1)
class Engine(object):
    def __init__(self, scene_map):
        self.scene_map = scene_map
    def play(self):
        current_scene = self.scene_map.opening_scene()
        last_scene = self.scene_map.next_scene('finished')
        while current_scene != last_scene:
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)
class Death(Scene):
    quips = [
    "Your dead",
    "rekt",
    "super rekt"]
    def enter(self):
        print(Death.quips[randint(0, len(self.quips)-1)])
        exit(1)
class Corridor(Scene):
    def enter(self):
        print("nYou've a gun and stuff, what will you do? Shoot/Leg it")
        action = input("n>> ")
        if action == "Shoot":
            print("nYou killed someone, good stuff")
            print("You get into the armory.")
            return 'laser_weapon_armory'
        elif action == "Leg it":
            print("nYou get shot before you get away.")
            return 'death'
        else:
            print("nThat was not an option")
            return 'central_corridor'
class LaserWeaponArmory(Scene):
    def enter(Self):
        print("nRight, you're in the armory.")
        print("There's a lock on the next door. nEnter 3 digits to unlock")
        code = "%d%d%d" % (randint(1,9), randint(1,9), randint(1,9))
        guess = input("n >> ")
        guesses = 0
        while guess != code and guesses < 10:
            print("nDENIED")
            guesses += 1
            guess = input("n >> ")
        if guess == code:
            print("nDoor opens.")
            print("You enter the bridge.")
            return 'the_bridge'
        else:
            print("nDoor remains locked. You get killed")
            Death()
class TheBridge(Scene):
    def enter(Self):
        print("nYou're in the bridge.")
        print("There's a bomb. What do you do?")
        print("Defuse/Run away")
        action = input("n >> ")
        if action == 'Defuse':
            print("nYou failed, bomb goes off")
            return 'death'
        elif action == 'Run away':
            print("nYou get in the escape pod bay.")
            return 'escape_pod'
        else:
            print("nThat was not an option.")
            Death()
class EscapePod(Scene):
    def enter(self):
        print("nThere's 5 pods, which do you take?")
        good_pod = randint(1.5)
        pod = input("n>>Pod #")
        if int(pod) != good_pod:
            print("nThis pod is fucked.")
            Death()
        else:
            print("nYurt, the pod works. You're out of here kid.")
            return 'finished'
class Finished(Scene):
    def enter(self):
        print("nGame over. Go away.")
        return 'finished'
class Map(object):
    scenes = {
    'central_corridor': Corridor(),
    'laser_weapon_armory': LaserWeaponArmory(),
    'the_bridge': TheBridge(),
    'escape_pod': EscapePod(),
    'finished': Finished(),
    }
    def __init__(self, start_scene):
        self.start_scene = start_scene
        print("start_scene in __init__", self.start_scene)
    def next_scene(self, scene_name):
        val = Map.scenes.get(scene_name)
        return val
    def opening_scene(self):
        return self.next_scene(self.start_scene)
    a_map = Map('central_corridor')
    a_game = Engine(a_map)
    a_game.play()

错误;

Traceback (most recent call last):
  File "C:PythonShtuffgame.py", line 143, in <module>
    a_game.play()
  File "C:PythonShtuffgame.py", line 20, in play
    next_scene_name = current_scene.enter()
AttributeError: 'NoneType' object has no attribute 'enter'

我对Python非常陌生,但基本上了解类和所有这些。据我所知,这与LPTHW中的代码完全相同。对于这个问题,我看到的两个答案都在我的代码中,所以我看不出我错在哪里。这是否与我使用Python3和LPTHW有关Python 2.XX?

对于不存在的键,字典的get方法返回None:

a = {'hello': 'world'}
print(a.get('hello')) # -> world
print(a.get('test'))  # -> None

你假设每个场景的enter方法返回下一个场景的名称。其中一些方法返回'death'(例如,参见CorridorTheBridge)。然后将这个返回值用作Map.scenes的键。但是这里没有'death'键,所以返回的是None


顺便说一下,selfSelf是完全不同的,但是你使用它们并期望它们做同样的事情。

最新更新