我很抱歉这个问题已被提出,并且多次回答。我已经检查了他们的大量问题,看到了确切的问题,并看到了他们的答案,但他们要么对我不起作用,要么是太简单的思维理解。这是我的代码;
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:\Python\Shtuff\game.py", line 143, in <module>
a_game.play()
File "C:\Python\Shtuff\game.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的事情有关吗?
答案 0 :(得分:1)
对于不存在的键,字典的None
方法返回a = {'hello': 'world'}
print(a.get('hello')) # -> world
print(a.get('test')) # -> None
:
enter
您假设每个场景的'death'
方法都返回下一个场景的名称。其中一些方法返回Corridor
(例如,请参阅TheBridge
和Map.scenes
)。然后,此返回值将用作'death'
的键。但是那里没有None
键,所以它返回self
。
顺便说一下,Self
和INSERT INTO..NOWAIT
是完全不同的,但是你同时使用它们并期望它们做同样的事情。