0

I have created a simple zork like game which uses text commands(more specifically in ex43 of learn python the hard way). I have written all the code and can break it to understand but the code does not execute. Here goes my code :

from sys import exit
from random import randint

class Scene(object):

    def enter(self):
        print "This 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()
        while True:
            print "\n-------------"
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)

class Death(Scene):
    quips = [
        'Shame on you. You lost!',
        'You are such a loser',
        'Even my dog is better at this'
        ]
    def enter(self):
        print Death.quips[randint(0, (len(self.quips) - 1))]
        exit(1) 


class CentralCorridor(Scene):

   def enter(self):
        print "The Gothoms of Planet Parcel # 25 have invade your ship and "
        print "killed all your crew members. You are the last one to survive."
        print "Your last mission is to get the neutron destruction bomb from "
        print "the Laser Weapons Armory and use it to destroy the bridge and get" 
        print "to the escape pod from where you can escape after putting in the" 
        print "correct pod."
        print "\n"
        print "Meanwhile you are in the central corridor where a Gothom stands"
        print "in front of you. You need to kill him if you want to proceed to" 
        print "the Laser Weapons Armory. What will you do now - 'shoot', 'dodge' or       'tell him a joke'"

        action = raw_input(">")

        if action == "shoot":
            print "You try to shoot him but he reacts faster than your expectations"
            print "and kills you"
            return 'death'
        elif action == "dodge":
            print "You thought that you will escape his vision. Poor try lad!"
            print "He kills you"
            return 'death'
        elif action == "tell him a joke":
            print "You seem to have told him a pretty funny joke and while he laughs"
            print "you stab him and the Gothom is killed. Nice start!"
            return 'laser_weapon_armory'
        else:
            print "DOES NOT COMPUTE!"
            return 'central_corridor'

class LaserWeaponArmory(Scene):

    def enter(self):
        print "You enter into the Laser Weapon Armory. There is dead silence"
        print "and no signs of  any Gothom. You find the bomb placed in a box"
        print "which is secured by a key. Guess the key and gain access to neutron "
        print "destruction bomb and move ahead. You will get 3 chances. "
        print "-----------"
        print "Guess the 3-digit key"
        key = "%d%d%d" % (randint(0, 9), randint(0, 9), randint(0, 9))
        guess_count = 0
        guess = raw_input("[keypad]>")
        while guess_count < 3 and guess!= key:
            print "That was a wrong guess"
            guess_count += 1
            print "You have %d chances left" % (3 - guess_count)
        if guess_count == key:
            print "You have entered the right key and gained access to he neutron"
            print "destruction bomb. You grab the bomb and run as fast as you can"
            print " to the bridge where you must place it in the right place"
            return 'the_bridge'
        else:
            print "The time is over and the Gothoms bomb up the entire ship and"
            print "you die"
            return 'death'

class TheBridge(Scene):

    def enter(self):
        print "You burst onto the bridge with the neutron destruction bomb"
        print "and surprise 5 Gothoms who are trying to destroy the ship "
        print "They haven't taken their weapons out as they see the bomb"
        print "under your arm and are afraid by it as they want to see the"
        print "bomb set off. What will you do now - throw the bomb or slowly place the bomb"
        action = raw_input(">")
        if action == "throw the bomb":
            print "In a panic you throw the bomb at the Gothoms and as you try to"
            print "escape into the door the Gothoms shot at your back and you are killed."
            print "As you die you see a Gothom trying to disarm the bomb and you die"
            print "knowing that they will blow up the ship eventually."
            return 'death'
        elif action == "slowly place the bomb":
            print "You inch backward to the door, open it, and then carefully"
            print "place the bomb on the floor pointing your blaster at it."
            print "Then you immediately shut the door and lock the door so"
            print "that the Gothoms can't get out. Now as the bomb is placed"
            print "you run to the escape pod to get off this tin can."
            return 'escape_pod'
class EscapePod(Scene):

    def enter(self):
        print "You rush through the whole ship as you try to reach the escape pod"
        print "before the ship explodes. You get to the chamber with the escape pods"
        print "and now have to get into one to get out of this ship. There are 5 pods"
        print "in all and all are marked from 1 to 5. Which escape pod will you choose?"
        right_pod = randint(1, 5)
        guess = raw_input("[pod#]>")

        if int(guess) != right_pod:
            print "You jump into the %s pod and hit the eject button" % guess
            print "The pod escape into the void of space, then"
            print "implodes as the hull ruptures crushing your"
            print "whole body"
            return 'death'
        elif int(guess) == right_pod:
            print "You jump into the %s pod and hit the eject button" % guess
            print "The pod easily slides out into the space heading to the planet below"
            print "As it flies to the planet you see the ship exploding"
            print "You won!"

            return "finished"

class Map(object):

    scenes = {
        'central_corridor' : CentralCorridor(),
        'laser_weapon_armory' : LaserWeaponArmory(),
        'the_bridge' : TheBridge(),
        'escape_pod' : EscapePod(),
        'death' : Death()
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene
    def next_scene(self, scene_name):
        return Map.scenes.get(scene_name)
    def opening_scene(self):
        return self.next_scene(self.start_scene)

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

I type the following command in cmd:

python ex43.py

But it doesn't print a single line nor does it give any error.This is what happens

D:\Python>python ex43.py
D:\Python>
8
  • Either your code above is not formatted in the same way as the code you're trying to run, or you have some indentation issues. Commented Apr 30, 2014 at 13:37
  • Please correct the indentation. It appears as if the methods of each of these classes are at global/file scope. Commented Apr 30, 2014 at 13:37
  • it was just a result of copy-paste it's corrected now Commented Apr 30, 2014 at 13:41
  • 3
    See line "a_game = Engine('a_map')", here should be "a_game = Engine(a_map)" Commented Apr 30, 2014 at 13:42
  • 1
    Are you certain there is no exception? Because you passed in a string to Engine(), which won't have a opening_scene attribute. Commented Apr 30, 2014 at 13:42

2 Answers 2

1

You are passing a str to Engine but it accepts a object. That's what this error is showing up. Just pass the object to Engine and you are good to go.

'str' object has no attribute 'opening_scene'

I fixed that for you. Now it works correctly.

from sys import exit
from random import randint


class Scene(object):
    def enter(self):
        print("This 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()
        while True:
            print("\n-------------")
            next_scene_name = current_scene.enter()
            current_scene = self.scene_map.next_scene(next_scene_name)


class Death(Scene):
    quips = [
        'Shame on you. You lost!',
        'You are such a loser',
        'Even my dog is better at this'
    ]

    def enter(self):
        print(Death.quips[randint(0, (len(self.quips) - 1))])
        exit(1)


class CentralCorridor(Scene):
    def enter(self):
        print("The Gothoms of Planet Parcel # 25 have invade your ship and ")
        print("killed all your crew members. You are the last one to survive.")
        print("Your last mission is to get the neutron destruction bomb from ")
        print("the Laser Weapons Armory and use it to destroy the bridge and get")
        print("to the escape pod from where you can escape after putting in the")
        print("correct pod.")
        print("\n")
        print("Meanwhile you are in the central corridor where a Gothom stands")
        print("in front of you. You need to kill him if you want to proceed to")
        print("the Laser Weapons Armory. What will you do now - 'shoot', 'dodge' or       'tell him a joke'")

        action = input(">")

        if action == "shoot":
            print("You try to shoot him but he reacts faster than your expectations")
            print("and kills you")
            return 'death'
        elif action == "dodge":
            print("You thought that you will escape his vision. Poor try lad!")
            print("He kills you")
            return 'death'
        elif action == "tell him a joke":
            print("You seem to have told him a pretty funny joke and while he laughs")
            print("you stab him and the Gothom is killed. Nice start!")
            return 'laser_weapon_armory'
        else:
            print("DOES NOT COMPUTE!")
            return 'central_corridor'


class LaserWeaponArmory(Scene):
    def enter(self):
        print("You enter into the Laser Weapon Armory. There is dead silence")
        print("and no signs of  any Gothom. You find the bomb placed in a box")
        print("which is secured by a key. Guess the key and gain access to neutron ")
        print("destruction bomb and move ahead. You will get 3 chances. ")
        print("-----------")
        print("Guess the 3-digit key")
        key = "%d%d%d" % (randint(0, 9), randint(0, 9), randint(0, 9))
        guess_count = 0
        guess = input("[keypad]>")
        while guess_count < 3 and guess != key:
            print("That was a wrong guess")
            guess_count += 1
            print("You have %d chances left" % (3 - guess_count))
        if guess_count == key:
            print("You have entered the right key and gained access to he neutron")
            print("destruction bomb. You grab the bomb and run as fast as you can")
            print(" to the bridge where you must place it in the right place")
            return 'the_bridge'
        else:
            print("The time is over and the Gothoms bomb up the entire ship and")
            print("you die")
            return 'death'


class TheBridge(Scene):
    def enter(self):
        print("You burst onto the bridge with the neutron destruction bomb")
        print("and surprise 5 Gothoms who are trying to destroy the ship ")
        print("They haven't taken their weapons out as they see the bomb")
        print("under your arm and are afraid by it as they want to see the")
        print("bomb set off. What will you do now - throw the bomb or slowly place the bomb")
        action = input(">")
        if action == "throw the bomb":
            print("In a panic you throw the bomb at the Gothoms and as you try to")
            print("escape into the door the Gothoms shot at your back and you are killed.")
            print("As you die you see a Gothom trying to disarm the bomb and you die")
            print("knowing that they will blow up the ship eventually.")
            return 'death'
        elif action == "slowly place the bomb":
            print("You inch backward to the door, open it, and then carefully")
            print("place the bomb on the floor pointing your blaster at it.")
            print("Then you immediately shut the door and lock the door so")
            print("that the Gothoms can't get out. Now as the bomb is placed")
            print("you run to the escape pod to get off this tin can.")
            return 'escape_pod'


class EscapePod(Scene):
    def enter(self):
        print("You rush through the whole ship as you try to reach the escape pod")
        print("before the ship explodes. You get to the chamber with the escape pods")
        print("and now have to get into one to get out of this ship. There are 5 pods")
        print("in all and all are marked from 1 to 5. Which escape pod will you choose?")
        right_pod = randint(1, 5)
        guess = input("[pod#]>")

        if int(guess) != right_pod:
            print("You jump into the %s pod and hit the eject button" % guess)
            print("The pod escape into the void of space, then")
            print("implodes as the hull ruptures crushing your")
            print("whole body")
            return 'death'
        elif int(guess) == right_pod:
            print("You jump into the %s pod and hit the eject button" % guess)
            print("The pod easily slides out into the space heading to the planet below")
            print("As it flies to the planet you see the ship exploding")
            print("You won!")

            return "finished"


class Map(object):
    scenes = {
    'central_corridor': CentralCorridor(),
    'laser_weapon_armory': LaserWeaponArmory(),
    'the_bridge': TheBridge(),
    'escape_pod': EscapePod(),
    'death': Death()
    }

    def __init__(self, start_scene):
        self.start_scene = start_scene

    def next_scene(self, scene_name):
        return Map.scenes.get(scene_name)

    def opening_scene(self):
        return self.next_scene(self.start_scene)


a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()

Output

-------------
The Gothoms of Planet Parcel # 25 have invade your ship and 
killed all your crew members. You are the last one to survive.
Your last mission is to get the neutron destruction bomb from 
the Laser Weapons Armory and use it to destroy the bridge and get
to the escape pod from where you can escape after putting in the
correct pod.


Meanwhile you are in the central corridor where a Gothom stands
in front of you. You need to kill him if you want to proceed to
the Laser Weapons Armory. What will you do now - 'shoot', 'dodge' or       'tell him a joke'
>
Sign up to request clarification or add additional context in comments.

5 Comments

so you changed just one thing??? and moreover I never got any error like 'str' object has no attribute 'opening_scene' i made the edit and still it doesn't simply show anything
@kartikey_kant The error shows up in my Python REPL.
ok but the code is not executing in cmd nor powershell..... even after making the edits
@kartikey_kant It should work too. It works fine in PowerShell and CMD too.
now i came to know why my code wasn't working even after changing Engine("a_map") to Engine(a_map)..... it was because the line exit(1) in the enter method of death class was not indented properly and the code exited every time at this point now it works fine...
0

ajkumar25's code works for me too. You can try to catch all exceptions to see, if you got any errors:

try:
    # all your code here
except Exception as e:
    print e
finally:
    raw_input("Press any key to exit...")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.