我正在开发一款游戏,我可以在其中使用char函数创建一个角色。tSt ()
应该代表角色在任何给定级别的统计数据。这是到目前为止的整个脚本。
不幸的是,它会导致:
TypeError : st () takes 1 argument but 2 were given.
我做错了什么?
g=1.08
h=1.1
j=1.12
mage = [g,g,g,j,h,h] # these are essentially classes of characters, affecting stat growth.
warrior = [h,j,h,g,g,h]
ranger = [h,g,g,g,h,j]
paladin = [j,h,g,h,g,h]
class char :
def __init__ (x,name,l,spec,stats):
x.name = name
x.spec = spec # class, as mentioned above by mage.
x.stats = stats # base stats.
x.l = l # level of character.
def n (x):
return (x.name)
def sp (x):
return x.spec
def lv (x):
return x.l
def st (x):
return x.stats
def tSt (x):
a=[]
for i in x.st():
a[i]=x.st([i])*x.sp([i])*(x.lv()-1)
return a # this is meant to change the stats to the appropriate value based on the level. It's meant to be equal to level*stat*modifier.
c1 = char ('andrè the giant mage',1,mage,[500,100,])
a = tSt (c1)
添加self
作为第一个参数。它应该是类的正常函数中的第一个参数。
def st (self, x):
return x.stats
您正在查看的成员函数,这些函数应该具有像st(self, x)
这样的签名。
如果您弯曲思想并认为给定一个类Foo
,也许它会有所帮助:
class Foo:
def a_method(self, x):
print(x)
如果创建实例foo
它:
foo = Foo()
然后打电话
foo.a_method("hello")
基本上是句法糖
Foo.a_method(foo, "hello")
很明显,您有两个参数要传递。
请将"self"
添加为类方法的第一个参数,例如:
def my_function(self, my_parameter)
这将允许您从整个类中访问方法和成员变量。