0

Well I am building a PyGame in Sublime text, but when I run the batch file to start it I get this error:


Traceback (most recent call last):
 File "game.py", line 12, in <module>
    player = player("Default", 1, 1, 1)
 File "C:\Users\*****\Desktop\RPG\characters\player.py", line 8, in __init__
    Character.__init__(self, name, hp)
 File "C:\Users\*****\Desktop\RPG\characters\character.py", line 9, in  __init__
player.dead = False
NameError: global name 'player' is not defined
Press any key to continue . . .

Now here are my files:

game.py file:


# Main Game File

from characters.player import *
from commands import *

commands = {
    'help': help,
    'exit': exit
 }
player = player("Default", 1, 1, 1)

def isValidCMD(cmd):
    if cmd in commands:
        return True
    return False

def runCMD(cmd, args, player):
    commands[cmd](player, args)

def main(player):
    while(not player.dead):
            line = raw_input(">> ")
        input = line.split()
        input.append("EOI")

        if isValidCMD(input[0]):
            runCMD(input[0], input[1], player)

        main(player)

player.py file:


# Player Base File

from character import *

class player(Character):
    def __init__(self, name, hp, str, int):
        Character.__init__(self, name, hp)
        self.str = str
        self.int = int

and the character.py file:


# Character Base File

class Character(object):
    def __init__(self, name, hp):
        self.name = name
        self.hp = hp

        player.dead = False

    def attack(self, other):
        pass

    def update(self):
        if self.hp <= 0:
            player.dead = True
            self.hp = 0

What do I do?

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
Miyte Pithon
  • 35
  • 2
  • 7
  • 1
    Possible duplicate of [Python: NameError: global name 'foobar' is not defined](http://stackoverflow.com/questions/4068785/python-nameerror-global-name-foobar-is-not-defined) – l'L'l Feb 06 '16 at 02:12

2 Answers2

1

1) Change

from characters.player import *

to

from player import * in game.py

2)In player.py, class player to class Player

and

3) in character.py, make this change please from

player.dead = False

to

self.dead = False

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
0

You can't have a variable with the same name as the package you're trying to access. Choose something other than player for the local variable.

Also, you'll still have a problem within Character.py, as there isn't anything named player defined in that file, and no global declared. I think what you want there is

self.dead = False

for the Character you're creating. player is a remote class with no dead property.


You cannot change just the one location: you have to change all of the uses for that aspect of the name. You have used player to refer to two different things: (1) a class with attributes str and int (these are poor names, by the way: there are built-in functions with those names, so these are not distinctive); and (2) a local object that appears to be an instance of class player.

There is no good way to use the same name for both. I suggest something more like

pov = player("Grmph the Magnetic", 1, "werewolf", 17)

You must also change the other references to the object player to be pov instead.


Also, you seem to be confused by the variety of errors in the current program. Please consider starting over; use incremental programming. Write a few lines -- a class definition with its init method, and a main program that instantiates and prints a single object. When that works, add a few lines: some meaningful initialization, perhaps. Continue adding and testing a few lines at a time until you get back to the level of complexity you have now.

This allows you to seal with only one or two errors at a time. You're already doing this about half-way, so I don't think it will take long to recover your current level of work ... but with a legal program.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • use player = Player or does it have to be completely different – Miyte Pithon Feb 06 '16 at 02:13
  • **player** is already the name of your class. You can change either that or the local variable. Capitalizing one is legal, but poor programming practice. Make it something descriptive and distinctive in each case. For instance, make your local variable **pov** or **avatar**, if that suits your mental space. Also, having things named both **character** and **characters** can lead to confusion. – Prune Feb 06 '16 at 02:16
  • so player = Player should be player = Pov or something like that – Miyte Pithon Feb 06 '16 at 02:20
  • I dont think I quite understand what i need to do entirely – Miyte Pithon Feb 06 '16 at 02:29