Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Class Question
#1
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:
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?
Quote
#2
The default argument should be at the end and no non-default argument should follow that.
Quote
#3
Can you elaborate a little? I'm pretty new to python and I'm doing this as a learning exercise. I'm not 100% familiar with terms yet
Quote
#4
take all the default arguments to the end of the function declaration like
def __init__(self, name, hp, mana, atkdmg, chardef baselevel=1,basedmg=0, basedef=0):
And call accordingly
Quote
#5
(May-22-2018, 09:41 AM)Rajesh1978 Wrote: take all the default arguments to the end of the function declaration like
def __init__(self, name, hp, mana, atkdmg, chardef baselevel=1,basedmg=0, basedef=0):
And call accordingly
Those are called keyword arguments - not default arguments; they have default values
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Quote
#6
aaahhhhh understoof. Thank you guys
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  class question jrauden 1 104 Feb-16-2020, 10:14 AM
Last Post: ibreeden
  First Post/ Class Error Question jvan1601 2 1,240 May-01-2017, 04:21 AM
Last Post: jvan1601

Forum Jump:


Users browsing this thread: 1 Guest(s)