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'
。我不知道该怎么做才能修复这个错误?
您似乎还没有初始化该类。尝试
rbox = Rbox()
rbox.run_sim(10)