Please help me to use Classes

This is the place for queries that don't fit in any of the other categories.

Please help me to use Classes

Postby Donovanx » Sat Jun 15, 2013 4:26 pm

Code: Select all
def Charack(self, Health, Defense, Strenght):
    #self.NPC = Npc
    self.Health = 10
    self.Defense = 12
    self.Strenght = 15

def Npc(race, Org, Elf):
    race.Org = ["10", "15", "20"]
    race.Elf = ["10", "20", "1"]

Print(Npc(0),(1))

I get this error
"Traceback (most recent call last):
File "C:\Documents and Settings\Owner\My Documents\Chars.py", line 11, in <module>
print(Npc(0),(1))
TypeError: Npc() missing 2 required positional arguments: 'Org' and 'Elf'"
Ok so I'm trying to get it so that I can call a race and call a skill of the race like health.
I know I had help with a class in another post but I don't know how to use is and this is a bit simpler but still good for now.
Any idee wat my problem is?
Fist time I'm getiing an error like this ad I'm missing something right infront of me
Ty guys for reading helping
Last edited by Yoriz on Sun Jun 16, 2013 2:04 pm, edited 1 time in total.
Reason: edited the title
Donovanx
 
Posts: 13
Joined: Fri May 10, 2013 1:54 pm

Re: me again = noob question

Postby Yoriz » Sat Jun 15, 2013 4:48 pm

Your code just looks weird are they supposed to be classes, methods or functions and you dont follow pep8 naming.
The actual error is because you are calling Npc with the wrong amount of arguments.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1052
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: me again = noob question

Postby Donovanx » Sat Jun 15, 2013 5:26 pm

Ty had no idee about the proper naming thing(pep8)
Anyhow fixed some of the code made it more readable to other people took out my lazy "class"
Made it how it should be (I think,stil learning classes)made Npc,npc
And now I get npc not defined?
I know its stil a bit wrong mind helping me out some more?
Code: Select all
class Character():
    def __init__(self, Health, Defense, Strenght):
        self.Health = 10
        self.Defense = 12
        self.Strenght = 15

    def npc(race, Org, Elf):
        race.Org = ["10", "15", "20"]
        race.Elf = ["10", "20", "1"]

print(npc(0),(1))
 
Donovanx
 
Posts: 13
Joined: Fri May 10, 2013 1:54 pm

Re: me again = noob question

Postby Yoriz » Sat Jun 15, 2013 5:44 pm

I've tried commenting on your code but its just not in a good way you need to learn how to use classes before using them.
Code: Select all
class Character():
    def __init__(self, Health, Defense, Strenght):  # these arguments are not
        #even used and should be lower case, did you mean to set passed in
        #value in which case you want
        #self.health = health (notice lower case)
        self.Health = 10
        self.Defense = 12
        self.Strenght = 15

    def npc(race, Org, Elf):  # what is this meant to be, it is doing nothing
        # inside the class it is just a normal function ie self is not used
        # arguments should be lower case
        race.Org = ["10", "15", "20"] # very strange race is passed in and
        #somehow Org is attached to it as a variable which you then set
        race.Elf = ["10", "20", "1"]

print(npc(0),(1))
# npc is supposed to be a method of Character, you have to create an instance of
# character and then call the npc method of that instance


I dont know what npc is meant to do but this is how to create instances of Character and call an instance variable
Code: Select all
class Character():
    def __init__(self, race, health, defense, strenght):
        self.race = race
        self.health = health
        self.defense = defense
        self.strenght = strenght

org = Character('Org', 10, 15, 20)
elf = Character('Elf', 10, 20, 1)

print(org.health)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1052
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: me again = noob question

Postby Donovanx » Sat Jun 15, 2013 6:56 pm

Ty once again your one of the few people willing to help someone new that's almost a lost cast.
Anyhow I belive I have wat I'm looking for but I have 2 more questions that should be easy for you
Code: Select all
class Character():
    def __init__(self, npc, health, defense, strenght):#method
        self.health = health
        self.defense = defense
        self.strenght = strenght
        self.npc = npc

npc = Character("org", "20", "30", "40")#instance
       #Character("elf", "10", "10", "10")

print(npc.npc, npc.health ,npc.defense, npc.strenght)

With the characters like org in this exsample is it posable to make it a list or should I do the whole prooces for evry character?(I'm sure there is a way I just have no idee how)
And how would I go about machinng strenght from an org against strenght/defense of an elf?
Ty again for taking up your time
Donovanx
 
Posts: 13
Joined: Fri May 10, 2013 1:54 pm

Re: me again = noob question

Postby Yoriz » Sat Jun 15, 2013 8:06 pm

If you want to store them in a list, just put them in a list.
Comparing one characters strength to another shown in the last line
Code: Select all
class Character():
    def __init__(self, npc, health, defense, strength):
        self.health = health
        self.defense = defense
        self.strength = strength
        self.npc = npc

npc = [Character("org", "20", "30", "40"),
       Character("elf", "10", "10", "10")]

print(npc[0].npc, npc[0].health, npc[0].defense, npc[0].strength)
print(npc[0].strength > npc[1].strength)
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1052
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: me again = noob question

Postby ochichinyezaboombwa » Sat Jun 15, 2013 8:15 pm

Answering your question, here is how one could create a list of characters:
Code: Select all
from random import choice, randrange
chars = []
for i in range(5):
    ch = Character(choice( ['elf', 'ogre', 'thing']) , randrange (100) , randrange (100) , randrange (100) )
    chars.append(ch)


And as I answered yours, please answer mine:
a) why do you type "20", "30", "40" instead of 20, 30, 40? what are you planning to do later with this data?
b) does "self. npc = nps ; ... npc = Character" not bother you at all?
c) if you don't know how to create an elementary data structure such as list why do you use classes?
d) if you don't know how to create an elementary data structure such as list why do you attempt creating a game?
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: snippsat and 2 guests