【发布时间】:2020-04-25 22:38:35
【问题描述】:
class Rbox:
""" Defining a class """
def __init__(self,num_parts=100):
self.num_parts=num_parts
self.list_particles=[0]*num_parts
def get_left_count(self):
print("the number of particles on the left: "+str(self.list_particles.count(0)))
return self.list_particles.count(0)
def get_right_count(self):
print("The number of particles on the right is: "+str(self.list_particles.count(1)))
return(self.list_particles.count(1))
def run_sim(self,time=1000):
for i in range(time):
var=int(random.random())*(self.num_parts)
if self.list_particles[var]==0:
self.list_particles[var]=1
if self.list_particles[var]==1:
self.list_particles[var]=0
我正在尝试运行Rbox.run_sim(10) 行,以运行通过随机过程将粒子从左侧盒子移动到右侧盒子的过程。但我不断收到错误'int' object has no attribute 'num_parts'。我不确定如何解决此错误?
【问题讨论】:
标签: python class object methods