Reputation: 3
I am currently working on this exercise from Learn Python the Hard way and am struggling with a little bit of an issue. Whenever I make it to the
LaserWeaponArmory(scene)
portion of my code, it will loop twice prior to executing the actual death scene. Essentially allowing me 20 attempts at guessing the door code as opposed to just 10. It will loop back to the start of the scene prior to the next 10 attempts. After that, it will exit properly.
Objects and classes are starting to make more sense to me, but I am just looking for some pointers in the direction of solving this issue at the moment. I would appreciate any advice anyone has to offer!
I have provided what I believe to be the associated code with my issue below, but I'm still a n00b, so if you need to see more to answer my question please let me know!
class Scene(object):
def enter(self):
print "This scene is not yet configured. Subclass it and implement enter()."
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)
# be sure to print out the last scene
current_scene.enter()
class Death(Scene):
quips = [
"You died. You kind suck at this.",
"Your mom would be proud...if she were smarter.",
"Such a luser.",
"I have a small puppy that's better at this."
]
def enter(self):
print Death.quips[randint(0, len(self.quips)-1)]
exit(1)
class LaserWeaponArmory(Scene):
def enter(self):
print "You do a dive roll into the Weapon Armory, crouch and scan the room"
print "for more Gothons that might be hiding. It's dead quiet, too quiet."
print "You stand up and run to the far side of the room and find the"
print "neutron bomb and it's container. There's a keypad lock on the box"
print "and you need the code to get the bomb out. If you get the code"
print "wrong 10 times then lock closes forever and you can't"
print "get the bomb. The code is 3 digits."
code = "%d%d%d" % (randint(1, 9), randint(1,9), randint(1,9))
guesses = 0
attempt = 1
print "Attempt number %s." % attempt
guess = raw_input("[keypad]> ")
while guess != code and guesses != 9:
print "BZZZZZZEDDD!"
guesses += 1
attempt += 1
print "Attempt number %s." % attempt
guess = raw_input("[keypad]> ")
if guess == code:
print "The container clicks open and the seal breaks, letting gas out."
print "You grab the neutron bomb and run as fast as you can to the"
print "bridge where you must place it in the right spot."
return 'the_bridge'
else:
print "The lock buzzes one last time and then you hear a sickening"
print "melting sound as the mechanism is fused together."
print "You decide to sit there, and finally the Gothons blow up the"
print "ship from their ship and you die."
return 'death'
Upvotes: 0
Views: 276
Reputation: 3251
In your Engine
loop you enter
each scene twice. The first time is the last line:
current_scene.enter()
The second time is when you re-enter the loop:
next_scene_name = current_scene.enter()
I think that last line is supposed to be outside the while
loop. Just un-indent it :)
Upvotes: 3