python - 汉诺塔类型错误 : 'type' object is not subscriptable

标签 python compiler-errors

我正在寻找一种算法来计算使用 OOP 为汉诺塔需要采取的步骤。

有两个类,棒和盘。我希望将磁盘作为每个棒实例中列表的一部分。

错误:

"TypeError: 'type' object is not subscriptable"



当我尝试将磁盘实例附加到棒实例中的列表中时,发生了上述错误。

我已经检查过什么是类型错误,但是我认为我没有使用任何 Python 的内置类型名称。
class rod:
    def __init__(self,disks=[]):
        self.diskslist=disks

    def remove_top(self):
        return self.disks.pop(-1)

    def add_to_top(self,disk):
        self.diskslist.extend(disk)

class disk:
    def __init__(self,size):
        self.size=size

disks=[]
for i in range(8):
    disks.append(disk(i))
#Generating 8 disks of increasing size

rods=[]
for i in range(3):
    rods.append(rod())
#Generating 3 rods

for i in range(len(disks)):
    rod[0].add_to_top(disks[I])
#Trying to add all the disks to the leftmost rod. This is where I get my type error.

最佳答案

代替

rod[0].add_to_top(disks[I])

采用
rods[0].add_to_top(disks[I])

( rods 而不是 rod )。

关于python - 汉诺塔类型错误 : 'type' object is not subscriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52792219/

相关文章:

xcode - 以前的定义在这里 - XCode 4.2.1 中的 Ghost 文件

Python 类静态方法

python - 在 Python 中,是否在执行新迭代之前重新评估循环中的条件?

python - 如何从 POST 请求中获取身份验证参数?

java - 删除了 Switch 语句以解决错误,但错误仍然存​​在。

asp.net-mvc - MVC 3.0缺少编译器所需的错误

python - 返回一个计算的 bool 值 pythonic 还是我应该使用传统的 if/else?

python - Keras 使用 set seed 得到不同的结果

c++ - libgomp缺少: install c++ compiler in home directory?

c++ - 如何修复C++中的 “expected”错误