Studying python and in the lesson it says to modify anc change the program. I thought I'd make the game more interactive. I know it requires the program to use inheritance and I've read everything and Ive tried everything. Ive run out of ideas and would appreciate any and all help. trying to make the game loop from the main class to the subclass where the player will fight against the aliens. When I try to make it loop to the submenu I keep getting the following error message.

This is the error I keep getting.
Traceback (most recent call last):

 in <module>
 Game().run()
 line 13, in run
 start = self.Scenes.get(start).enter()
AttributeError: 'NoneType' object has no attribute 'enter'

This is the program.
It stops right when I try and make it go to the submenu of the game.

import random
class Game(object):
    def __init__(self):
        self.Scenes={"Intro":Intro(),"CorridorRoom":CorridorRoom(),"EngineRoom":EngineRoom(),
                     "ArmouryRoom":ArmouryRoom(),"EscapePod":EscapePod(),
                     "Bridge":Bridge(),"scene":dead()}

    def run(self):
        start="Intro"
        while True:
            start = self.Scenes.get(start).enter()

class Scene(object):
    def enter(self):
        pass
        exit(0)

class State(Scene):        
    def state(self):
        pass
        exit(0)

class state(Game):
    def __init__(self):
        self.player_hp = 10
        self.chicken_hp = 10

def scene_intro(state):
    print "Player vs chicken...."
    print "Fight!!! "
    return "scene_initiative"


def scene_initiative(state):
    print"Lets see who attacks first "
    chicken, player = roll(6),roll(6)
    print "Chickens rolls",chicken
    print "Player rolls",player
    if chicken<player:
        print "Player wins initiative "
        raw_input()
        return scene_player
    elif player<chicken:
        print "Chicken wins initiative "
        raw_input()
        return scene_chicken
    else:
        print"Even numbers, lets start again..."
        raw_input()
        return scene_initiative

def scene_player(state):
    damage = roll(10)
    print "The chicken takes", damage, "points of damage"
    state.chicken_hp -= damage
    if state.chicken_hp <=0:
        print "Looks like we're having chicken tonight "
        return None
    print"You have",str(state.player_hp),"health left"
    return scene_initiative


def scene_chicken(state):
    damage = roll(10)
    print "You took", damage, "damage"
    state.player_hp -= damage
    if state.player_hp <=0:
        print "That hurt."
        return scene_dead
    print"You have",str(Game.player_hp),"health left"
    return scene_initiative

def scene_dead(state):
    print "You died, I bet you're so smart you have a pet zebra and call him spot"
    print"Do you want to play again? <yes/no>"
    exitgame=raw_input()
    if exitgame in ("no","n"):
        return "Intro"
    elif exitgame in ("yes","ys"):
        exit(0)

class dead(Scene):
    def enter(self):
        print "You died, I bet you're so smart you have a pet zebra and call him spot"
        print"Do you want to play again? <yes/no>"
        exitgame=raw_input()
        if exitgame in ("no","n"):
            return "Intro"
        elif exitgame in ("yes","ys"):
            exit(0)

class Intro(Scene):
    def enter(self):
        print intro
        choice=raw_input()
        choice=choice.lower()
        if choice in ("yes","ys","y"):
            return "CorridorRoom"
        elif choice in ("no","n"):
            exit(0)
        else:
            print "Choose one of the options "
            return"Intro"

class CorridorRoom(Scene):
    def enter(self):
        print a
        opts=raw_input()
        if opts in ('1','armoury','armouryroom','armoryroom'):
            return "ArmouryRoom"
        elif opts in ('2','engine room'):
            return "EngineRoom"
        elif opts in ('3','escape','escape pod'):
            print "Silly rabbit, trix are for kids, you need to blow the ship up before you leave. \n" 
            return "CorridorRoom"
        else:
            print"Uh oh looks like someone doesn't know how to read.\
Do we need to send you back to elementry school to teach you how?\n\
Choose one of the options\n "
            return "CorridorRoom"

class EngineRoom(Scene):
    def enter(self):
        print engineroom
        er=raw_input()
        if er =='1':
            return scene_intro
        elif er =='2':
            print "You sneek up to a Gothon, shoot and kill the first one,"
            print "just as the second one attacks you "
            return "scene_intro"
        elif er =='3':
            print "You sneek around the engine room avoiding the Gothons."
            print "Arriving at the engine rooms main computer you start downloading the self destruct code "
            print "When suddenly the computer makes a beeping noise then says 'down load complete' "
            print "1.Do you grab the chip?\n2.Hide\n3.attack the Gothons"
            a=raw_input()
            if a =='1':
                print "You grab the chip and at the same time the Gothons see you and attack "
                return "scene_intro"
            elif a =='2':
                print "you quickly duck behind the computer and the Gothon doesn't see you "
                return "CorridorRoom"
            elif a =='3':
                return "scene_intro"

        elif er =='4':
            return "ArmouryRoom"
        else:
            print "Select one of the options"
            return "EngineRoom"


class ArmouryRoom(Scene):
    def enter(self):
        pass

class EscapePod(Scene):
    def enter(self):
        pass

class Bridge(Scene):
    def enter(self):
        pass

engineroom="""You enter the engine room and you see two alien chickens.
will you:
1.Attack the Gothons?
2.Sneek up and shoot them?
3.Sneek around the room and grab the self destruct code from the engine room computer?
4.Grab a weapon from the armoury and come back armed?.
"""
intro="""Alien chickens known as Gothons have invaded your spaceship led by the evil Col
Sanders and our hero has to go through a maze of rooms defeating
them so he can escape into an escape pod to the planet below. 
Let's play Gothons From Planet Percal #25.
 <yes/no>\n """
a="""You're in a large corridor with two doors on either side of corridor
you are currently inside...
what do you want to do?:
1.Go to the armoury to grab a weapon?\n2.Go to the engine room to get the self destruct code?
3.Go to the escape pod?
"""
"""
for i in range(len(a)):
            stdout.write(a[i])
            stdout.flush()
            sleep(.1)

for i in range(len(engineroom)):
            stdout.write(engineroom[i])
            stdout.flush()
            sleep(.1)

for i in range(len(intro)):
            stdout.write(intro[i])
            stdout.flush()
            sleep(.1)

"""
Game().run()

Recommended Answers

All 8 Replies

Try to replace self.Scenes.get(start).enter() with self.Scenes[start].enter() .

It didn't work. Do you have any other ideas?.

You say it didn't work, but what is the error message ?

When I select to fight the alien I get the following error message:

Traceback (most recent call last):
line 180, in <module>
Game().run()
line 10, in run
start = self.Scenes[start].enter()
KeyError: <function scene_intro at 0x02C28D30>

At line 127 above, the code returns scene_intro instead of "scene_intro". There is a high probability that this is your bug.

I fixed that error but I still have an error but this time it says...

Traceback (most recent call last):
 line 180, in <module>
    Game().run()
   line 10, in run
    start = self.Scenes[start].enter()
KeyError: None

I know in theory my program would work and I know the problem is something I over looked.

We're going to track the moment where the start variable becomes None. Replace the run() method with this one

    def run(self):
        start="Intro"
        while True:
            next_start = self.Scenes[start].enter()
            if next_start is None:
                raise RuntimeError(('Enter method returned None:', self.Scenes[start].enter))
            else:
                start = next_start

Edit

Now I'm getting this error.

Traceback (most recent call last):
line 185, in <module>
    Game().run()
 line 11, in run
    next_start = self.Scenes[start].enter()
KeyError: 'scene_intro'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.