【问题标题】:How do i fix my class code 'int' object error?如何修复我的类代码“int”对象错误?
【发布时间】: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


    【解决方案1】:

    看来你还没有初始化这个类。试试

    rbox = Rbox()
    rbox.run_sim(10)
    

    【讨论】:

    • 谢谢!我以为我以前做过,但现在可以了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 2019-12-04
    • 2021-11-29
    • 2020-04-23
    • 2021-12-06
    相关资源
    最近更新 更多