【发布时间】:2013-11-22 23:45:05
【问题描述】:
我希望你们能在这里帮助我。 我从以下代码中得到了这个错误:
Traceback (most recent call last):
File "C:\Python27\Lib\idlelib\Tarea5.py", line 60, in <module>
bg.addBandit(b)
TypeError: unbound method addBandit() must be called with BanditGroup instance as first argument (got classobj instance instead)
代码:
from numpy import *
from matplotlib import pyplot as p
class Bandit:
power = random.uniform(15,46)
life = random.uniform(40,81)
def __init__(self, power, life):
self.power = power
self.life = life
class BanditGroup:
def __init__(self,a):
self.group = [a] #Where 'a' is an object of the class Bandit
def addBandit(self,b):
self.group.append(b) #Where 'b' is an object of the class Bandit
return self.group
howmanygroups = random.randint(4,11)
i = 0
j = 0
while i <= howmanygroups:
bg = BanditGroup
howmanybandits = random.randint(1,11)
while j <= howmanybandits:
b = Bandit
bg.addBandit(b) #<-- line 60
j+=1
bgposx = random.uniform(0,50000)
bgposy = random.uniform(0,50000)
p.plot(bgposx,bgposy,'r^')
i+=1
如果有人能告诉我这里发生了什么,我将不胜感激。我大约 2 个月前开始学习 python 2.7。 谢谢!
【问题讨论】:
-
你实际上并没有制作任何强盗或一群强盗来放置它们。
Bandit和BanditGroup是类;您的代码现在尝试做的类似于尝试坐在椅子的抽象概念上,而不是去宜家并找一把特定的椅子坐下。
标签: python class python-2.7 methods matplotlib