*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("\t\tNew Game")
    print("\t\tLoad Game")
    print("\t\tExit Game")
    input("\t\t\n\n ")
    if input == "new game":
    elif input == "load game":
    elif input == "exit game":
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)
        return self.inv

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

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

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.
Posts: 41
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.
Due to the reasons discussed here we will be moving to python-forum.io/ on October 1 2016
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 1670
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:
Code: Select all
def _init_

Code: Select all
def __init__
we will be moving to python-forum.io on October 1 2016
more details here
User avatar
Posts: 2226
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Return to Game Development

Who is online

Users browsing this forum: No registered users and 2 guests