python - 如何修复我的类代码“int”对象错误?
问题描述
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)
推荐阅读
- javascript - 如何创建今天的日期和一年前的日期
- typescript - 如何从对象中提取特定类型的键
- docker - Using Docker Buildkit --mount=type=cache for caching Nuget packages for .NET 5 dockerfile
- c# - 使用 .NET LDAPConnection 执行绑定到 LDAP 实例
- mqtt - 如何使用 libmosquitto 库在连接时检测到与 MQTT 代理的连接中的用户/密码问题?
- c++ - XCode 没有看到新的 C++17 功能
- javascript - My Javascript scroll function is not working?
- spring-batch - 跳过spring-batch中的异常并在数据库中提交错误
- excel - Excel VBA - 根据值更改单元格颜色
- javascript - 由于另一个选项而更改选择选项