5

I'm messing around with Class' and I have a python3 file with the following code:

class playerstats(object):
def __init__(self, name, baselevel=1, hp, mana, basedmg=0, atkdmg, basedef=0, chardef):
    self.name = name
    self.baselevel = baselevel
    self.hp = hp
    self.mana = mana
    self.basedmg = basedmg
    self.atkdmg = atkdmg
    self.basedef = basedef
    self.chardef = chardef
    return self.name, self.baselevel, self.basedmg, self.basedef
def selectedclass(self, chosenclass):
    if chosenclass == 'W' or chosenclass == 'w':
        self.hp = 100
        self.mana = 50
    elif chosenclass == 'M' or chosenclass == 'm':
        self.hp = 75
        self.mana = 100
    else:
        print('Error')
    return self.hp, self.mana

charcreation = playerstats('Tom', baselevel, self.chosenclass, self.chosenclass, basedmg, 0, basedef, 0)

self.chosenclass = 'w'

print(playerstats.hp)

When I run it, I get this Error: File "..\Playground\", line 2 def init(self, name, baselevel=1, hp, mana, basedmg=0, atkdmg, basedef=0, chardef): ^ SyntaxError: non-default argument follows default argument

Can someone help me understand why?

1 Answer 1

8

You need to change the order of parameters in __init__ function.

Generally, non-default argument should not follow default argument , it means you can't define (a="b",c) in function.

The order of defining parameter in function are :

  • positional parameter or non-default parameter i.e (a,b,c)
  • keyword parameter or default parameter i.e (a="bcd",r="jef")
  • keyword-only parameter i.e (*args)
  • var-keyword parameter i.e (**kwargs)
Sign up to request clarification or add additional context in comments.

3 Comments

@esteel: You need any more information or clarification ?
@esteel: Have you tested with above changes ?
I have and it worked but I got another error after that. Will post when I get home

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.