Python Forum
Class attribute not recognized\working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class attribute not recognized\working
#1
Hi, me again. 
 
I'm having trouble getting the attack function to work. The problem seems to be with the 'health' attribute within the Alien class (line 26 in the script). I can’t get this attribute to be recognized within attack() (line 238 in the script). I have tried the following to no avail:
 
·        Calling the ‘an_alien’ instance
·        Using the Alien class directly (which is where I was going wrong previously with something else, so doubt this is correct) – so line 8 would have Alien.health
·        Creating another instance entirely within attack()
 
Calling the instance 'an_alien' (using it as a reference) returns the following error:


Error:
Traceback (most recent call last):   File "ex43_mine.py", line 256, in <module>     a_game.play()   File "ex43_mine.py", line 46, in play     next_scene_name = current_scene.enter()   File "ex43_mine.py", line 86, in enter     attack()   File "ex43_mine.py", line 238, in attack     while hero_health > 0 and an_alien.health < 0: NameError: global name 'an_alien' is not defined
Here’s the entire script. I’m guessing it’s something glaringly obvious I’m missing. Any help\hints you can provide would be great - thanks!

from sys import exit
from random import randint
import random

class Scene(object):

    def enter(self):
        print """This scene is not yet configured. Subclass it and implement
        enter()."""
        exit(1)

class Alien(object):

    aliens = {
        'central_corridor': 'puppeteer',
        'laser_weapon_armory': 'kzin',
        'the_bridge': 'trooper',
        'escape_pod': 'predator',
    }

    weapons = ['feather', 'tasp', 'light_sabre', 'grenade']

    def __init__(self, alien_name, alien_lair):
        self.alien_name = alien_name
        self.alien_lair = alien_lair
        self.health = 2

    # loads corresponding alien onto scene
    def load_alien(self, alien_lair):
        alien = self.aliens.get(alien_lair)
        return alien

    ##attack or miss (use random)

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 kinda suck at this.",
        "Your mom would be proud... if she were smarter.",
        "Such a loser.",
        "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 CentralCorridor(Scene):

    def enter(self):
        an_alien = Alien('puppeteer', 'central_corridor')
        loaded_alien = an_alien.load_alien('central_corridor')

        print "The Gothons of Planet Percal #25 have invaded your ship and destroyed"
        print "your entire crew.  You are the last surviving member and your last"
        print "mission is to get the neutron destruct bomb from the Weapons Armory,"
        print "put it in the bridge, and blow the ship up after getting into an "
        print "escape pod."
        print "\n"
        print "You're running down the central corridor to the Weapons Armory when"
        print "a %s high on crack jumps out, both heads bobbing to and fro" % loaded_alien,
        print "He's blocking the door to the Armory and about to blast you. Quick"
        print ", choose a weapon from your arsenal."

        selected_weapon = random.choice(an_alien.weapons)

        if selected_weapon == 'feather':

            attack()

            print "Quick on the draw you yank out your feather and tickle the Puppeteer"
            print "once he re-emerges from his perfectly curled sphere. Once he is"
            print "disposed, you go through the Armory door."
            return 'laser_weapon_armory'

        else:
            print "A %r is not going to help you! The Puppeteer proceeds" % selected_weapon
            print "to then kick you you're a$$ to death. You lose!"
            return 'death'

class LaserWeaponArmory(Scene):

    def enter(self):
        loaded_alien = an_alien.load_alien('laser_weapon_armory')
        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 in its container.  Before you approach the keypad lock"
        print "on the box, you notice a orange-haired, bi-ped tiger, grinning from"
        print "ear to ear with his razor-sharp incisors. A %s. " % loaded_alien,
        print "You need the code to get the bomb out, but need to go through a kzin"
        print "to be successful. If you get the code wrong 10 times then the lock"
        print "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))
        guess = raw_input("[keypad]> ")
        guesses = 0

        while guess != code and guesses < 10:
            print "BZZZEDDD!"
            guesses += 1
            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'

class TheBridge(Scene):

    def enter(self):
        loaded_alien = an_alien.load_alien("the_bridge")
        print "You burst onto the Bridge with the netron destruct bomb"
        print "under your arm and surprise a %s who is trying to" % loaded_alien,
        print "take control of the ship. He's reaching for his gun as he sees the"
        print "active bomb under your arm and doesn't want to set it off."

        action = raw_input("> ")

        if action == "throw the bomb":
            print "In a panic you throw the bomb at the group of Gothons"
            print "and make a leap for the door.  Right as you drop it a"
            print "Gothon shoots you right in the back killing you."
            print "As you die you see another Gothon frantically try to disarm"
            print "the bomb. You die knowing they will probably blow up when"
            print "it goes off."
            return 'death'

        elif action == "slowly place the bomb":
            print "You point your blaster at the bomb under your arm"
            print "and the Gothons put their hands up and start to sweat."
            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 "You then jump back through the door, punch the close button"
            print "and blast the lock so the Gothons can't get out."
            print "Now that the bomb is placed you run to the escape pod to"
            print "get off this tin can."
            return 'escape_pod'

        else:
            print "DOES NOT COMPUTE!"
            return "the_bridge"

class EscapePod(Scene):

    def enter(self):
        loaded_alien = an_alien.load_alien('predator')
        print "You rush through the ship desperately trying to make it to"
        print "the escape pod before the whole ship explodes.  It seems like"
        print "hardly any Gothons are on the ship, so your run is clear of"
        print "interference... until you see the %s. You get to the" % loaded_alien,
        print "the escape pods, and now need to pick one to take.  Some of them"
        print "could be damaged but you don't have time to look.  There's 5"
        print "pods, which one do you take?"

        good_pod = randint(1,5)
        guess = raw_input("[pod #]> ")


        if int(guess) != good_pod:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod escapes out into the void of space, then"
            print "implodes as the hull ruptures, crushing your body"
            print "into jam jelly."
            return 'death'

        else:
            print "You jump into pod %s and hit the eject button." % guess
            print "The pod easily slides out into space heading to"
            print "the planet below.  As it flies to the planet, you look"
            print "back and see your ship implode then explode like a"
            print "bright star, taking out the Gothon ship at the same"
            print "time.  You won!"

            return 'finished'

class Finished(Scene):

    def enter(self):
        print "You won! Good job."
        return 'finished'

class Map(object):

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


    def __init__(self,  start_scene):
            self.start_scene = 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)


def attack():
    hero_health = 3
    success = ['hit', 'miss']
    #an_alien = Alien('puppeteer', 'central_corridor')
    # random generator between hit and miss for both hero and alien
    hero_attack_attempt = random.choice(success)
    alien_attack_attempt = random.choice(success)
    while hero_health > 0 and an_alien.health < 0:
        if hero_attack_attempt == 'hit':
            an_alien.health -= 1
            print "Direct hit. Go on with yo' bad self."

        elif alien_attack_attempt == 'hit':
            hero_health -= 1
            print "Ouch! You got hit!"

        else:
            print "You missed. The alien is too adept at defending itself."

    while hero_health <= 0:
        return Death
        #or 'death'

a_map = Map('central_corridor')
a_game = Engine(a_map)
a_game.play()
Reply
#2
an_alien is created as a local instance of Alien in the enter method of CentralCorridor, it is not accessable outside of this method.
If you want this instance to be accessible create an instance that you passed to other objects that require to use it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  is import cointegration_analysis a recognized module mitcht33 1 424 Nov-06-2023, 09:29 PM
Last Post: deanhystad
  The term 'pip' is not recognized as the name of a cmdlet, function michaelnicol 1 624 Jul-16-2023, 11:12 PM
Last Post: deanhystad
  Initiating an attribute in a class __init__: question billykid999 8 1,310 May-02-2023, 09:09 PM
Last Post: billykid999
  Index Function not recognized in Python 3 Peter_B_23 1 1,200 Jan-08-2023, 04:52 AM
Last Post: deanhystad
  class, attribute and method Frankduc 9 2,454 Feb-27-2022, 09:07 PM
Last Post: deanhystad
  Python generics: How to infer generic type from class attribute? Thoufak 0 2,809 Apr-25-2021, 09:31 AM
Last Post: Thoufak
  AttributeError class object has no attribute list object scttfnch 5 3,417 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  "can't set attribute" on class DreamingInsanity 2 10,846 Aug-22-2020, 07:57 PM
Last Post: DreamingInsanity
  TypeError: size; expecting a recognized type filling string dict a11_m11 0 2,514 Feb-10-2020, 08:26 AM
Last Post: a11_m11
  matplotlib isn't recognized after installation Pavel_47 5 2,790 Sep-18-2019, 07:01 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020