*Beginner* - Having trouble understanding OOP (self class)

*Beginner* - Having trouble understanding OOP (self class)

Postby mdovey » Sun Jul 20, 2014 3:38 pm

Hello I am a beginner at using python and have just started trying to learn it. I have only recently ran into using classes to create objects and using the self function within that. To try and learn / get the hang of python I am trying to produce a very simple text based RPG. Unfortunately as I am new I am trying to implement features such as OOP into this which I have only just learnt about. This has led me running into a few hurdles and errors which I do not understand. I am currently trying to run my code to see if the main menu will load up but I am being stopped as python is flagging up an open bracket within my first class object. I am being presented with a pop up telling me that a syntax error is occurring due to "non-default argument follows default argument". I would be grateful if anyone could shine some light on what is wrong. I will include all of my code so that people are able to give any advice such as telling me I am using the complexly wrong methods to go about what I am looking to achieve.

(the syntax error is occuring in the "Class player(object)" block of code right after def _init_ on the opening bracket)
Code: Select all
def menu():
    print("Hero\n\n")
    print("\t\tNew Game")
    print("\t\tLoad Game")
    print("\t\tExit Game")
    input("\t\t\n\n ")
    if input == "new game":
        newgame()
    elif input == "load game":
        loadgame()
    elif input == "exit game":
        exit
    else:
        menu()
       
class player(object):
    def _init_(self, name, attack = 5, defence = 5, health = 100, glory = 0, gold = 0, items):
        self.name = name
        self.attack = attack
        self. defence = defence
        self.health = health
        self.glory = glory
        self.gold = gold
        self.items = items
        self.inv = ["dagger"]
        for item in items:
            add_item = contents(item)
            self.inv.append(add_item)
        return self.inv

    def _str_(self):
        score = self.name + ":\t" + str(self.glory)
        return score

def invent():
    print("Inventory\n\n")
    print("You have", len(self.inv), "item.")
    print("\nItems: \n")
    print(self.inv)


def character():
    print("\t\tPlease create a name for your hero\n")
    name = input("\t\tName: ")



class shop(object):
    def _init_(self, goods, cost,):
        self.goods = goods
        self.cost = cost
        goods = ["shield", "helmet", "chest piece", "leggings" "health potion"]

class goblin(object):
    def _init_(self, name, health, attack, defence):
        self.name = name
        self.health = health
        self.attack = attack
        self.defence = defence
Last edited by micseydel on Sun Jul 20, 2014 4:29 pm, edited 1 time in total.
Reason: First post lock.
mdovey
 
Posts: 15
Joined: Sun Jul 20, 2014 3:30 pm

Re: *Beginner* - Having trouble understanding OOP (self clas

Postby Yoriz » Sun Jul 20, 2014 4:56 pm

In the line
Code: Select all
def _init_(self, name, attack = 5, defence = 5, health = 100, glory = 0, gold = 0, items):

non-default argument (items) follows default argument (gold)
You either need to give items a default value or move it to before the first parameter that uses a default agument, once you have a parameter using a default argument you can not follow with a non-default argument.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1027
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: *Beginner* - Having trouble understanding OOP (self clas

Postby metulburr » Sun Jul 20, 2014 8:16 pm

also it is two underscore each side, not one:
change
Code: Select all
def _init_

to
Code: Select all
def __init__
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1502
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to Game Development

Who is online

Users browsing this forum: Bing [Bot] and 2 guests