3 Replies - 767 Views - Last Post: 18 October 2014 - 04:11 PM Rate Topic: -----

#1 Mike592   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-October 14

Undefined Variable when trying to initialize object!

Posted 18 October 2014 - 03:42 PM

Here is my main code:
"""File that starts the game"""
import Classes
import Functions
#Space for imports

PlayerRace = input("What race are you? ")
PlayerName = input("What is your name? ")

Player = Character(PlayerName, PlayerRace, 25, 1)



This imports this code with the class in it:

"""File with all classes"""
import Variables

class Inventory(object):
  
  def __init__(self, name, size):
    self.name = name
    self.size = size
  
  contents = {}
  
  def add_item(self, item, inventory):
    inventory.append(item)
    addedtext = item + " recieved!"
    print addedtext
  
  def remove_item(self, item, inventory):
    if item in inventory:
      inventory.remove(item)
      removeitem = item + " removed!"
      print removeitem

class Organism(object):
  
  def __init__(self, name, species, extra_health, extra_damage):
    self.name = name
    self.species = species
    self.health = 25 + extra_health
    self.damage = 1 + extra_damage
  
  isdead = False
  
  def dead(self, name, health):
    if self.health <= 0:
      isdead = True
      return True
    elif self.health > 0:
      isdead = False
      return False

class Enemy(Organism):
  
  def __init__(self, name, species, extra_health, extra_damage):
    self.name = name
    self.species = species
    self.health = 25 + extra_health
    self.damage = 1 + extra_damage

class Character(Organism):
  
  def __init__(self, name, species, extra_health, extra_damage):
    self.name = name
    self.species = species
    self.health = 25
    self.damage = 1 + extra_damage
  
  MaxHealth = 50



The error I get is this:

Traceback (most recent call last):
File "/home/ubuntu/workspace/python/Main.py", line 9, in <module>
Player = Character(PlayerName, PlayerRace, 25, 1)
NameError: name 'Character' is not defined

So can someone tell what went wrong and what to do to solve this problem?

Is This A Good Question/Topic? 0
  • +

Replies To: Undefined Variable when trying to initialize object!

#2 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Undefined Variable when trying to initialize object!

Posted 18 October 2014 - 03:52 PM

import Classes makes the classes available, but you still need to reference the module:
Player = Classes.Character(PlayerName, PlayerRace, 25, 1)

Alternatively, you can use:
from Classes import *

or
from Classes import Inventory, Organism, Enemy, Character

This post has been edited by andrewsw: 18 October 2014 - 03:54 PM

Was This Post Helpful? 1
  • +
  • -

#3 Mike592   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 18-October 14

Re: Undefined Variable when trying to initialize object!

Posted 18 October 2014 - 03:57 PM

View Postandrewsw, on 18 October 2014 - 03:52 PM, said:

from Classes import *

That works thank you so much!!!!
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Undefined Variable when trying to initialize object!

Posted 18 October 2014 - 04:11 PM

You're welcome.

Bear in mind that using * to import everything should be used sparingly; it's okay with your Classes as there are only four imports.

An exception would be for a GUI, e.g. from tkinter import *, where it is sensible/convenient to load everything.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1