很抱歉,此問題已被詢問並多次回答。我已經檢查了他們中的大部分,確切的問題並且看到了他們的答案,但是他們要麼對我不起作用,要麼對我的簡單理解來說太沉重。這是我的代碼;學習Python困難的方式ex43
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的事情嗎?
[爲什麼學習Python很難,當你可以輕鬆學習!](http://sopython.com/wiki/LPTHW_Complaints) –