【问题标题】:TypeError: '>' not supported between instances of 'Block' and 'Block'?TypeError:“块”和“块”的实例之间不支持“>”?
【发布时间】:2017-08-02 01:36:36
【问题描述】:

我想使用insert_animate(blockposition, shelf, high) 定义一个函数sortbooks(shelf),以便它根据货架的大小按升序对货架进行排序。

我想出了这个功能:

def sortbooks(shelf):
    for i in shelf:
        insert_animate(i.size,shelf,max(shelf))
    return shelf

但是,我得到了一个 TypeError:

File "<pyshell#275>", line 3, in sortbooks
insert_animate(i.size,shelf,max(shelf))
TypeError: '>' not supported between instances of 'Block' and 'Block'

我应该如何定义这个函数?我的方法完全错误吗?我真的很感激帮助/一些指导!谢谢!

这是insert_animate的函数:

def insert_animate(blockposition, shelf, high):
    if blockposition == 0:
        return shelf
    a = s.pop(blockposition)
        for i in range(high):
            if a.size <= s[i].size:
                s.insert(i, a)
                break
    else:
        s.insert(high, a)
    return shelf

下面是shelf.py

from turtle import *

class Block(Turtle):
    def __init__(self, size):
        self.size = size
        Turtle.__init__(self, shape="square", visible=False)
        self.pu()
        self.shapesize(size * 1.5, 1.5, 2) # square-->rectangle
        self.fillcolor("black")
        self.st()
    def glow(self):
        self.fillcolor("red")
    def unglow(self):
        self.fillcolor("black")
    def __repr__(self):
        return "Block size: {0}".format(self.size)

class Shelf(list):
    def __init__(self, y):
        "create an shelf. y is y-position of first block"
        self.y = y
        self.x = -150
    def push(self, d):
        width, _, _ = d.shapesize()
        yoffset = width/2 * 20 # to align the blocks by it's bottom edge
        d.sety(self.y + yoffset)
        d.setx(self.x+34*len(self))
        self.append(d)
    def _close_gap_from_i(self, i):
        for b in self[i:]:
            xpos, _ = b.pos()
            b.setx(xpos - 34)
    def _open_gap_from_i(self, i):
        for b in self[i:]:
            xpos, _ = b.pos()
            b.setx(xpos + 34)
    def pop(self, key):
        b = list.pop(self, key)
        b.glow()
        b.sety(200)
        self._close_gap_from_i(key)
        return b
    def insert(self, key, b):
        self._open_gap_from_i(key)
        list.insert(self, key, b)
        b.setx(self.x+34*key)
        width, _, _ = b.shapesize()
        yoffset = width/2 * 20 # to align the blocks by it's bottom edge
        b.sety(self.y + yoffset)
        b.unglow()

def show_text(text):
    goto(0,-250)
    write(text, align="center", font=("Courier", 16, "bold"))

def start_sort():
    onkey(None,"space")
    clear()
    show_text("sort_me")
    sort_func(s)

def init_shelf(vals=(4, 8, 2, 9, 3, 1, 10, 7, 5, 6)):
    s = Shelf(-200)
    for i in vals:
        s.push(Block(i))
    return s

def clear_window():
    getscreen().clearscreen()

def main(func):
    global sort_func
    sort_func = func
    getscreen().clearscreen()
    ht(); penup()
    init_shelf()
    show_text("press spacebar to start sorting")
    onkey(start_sort, "space")
    onkey(bye, "Escape")
    listen()
    mainloop()

【问题讨论】:

    标签: python python-3.x sorting typeerror turtle-graphics


    【解决方案1】:

    要解决您现在遇到的问题,您可以改用max(shelf, key=lambda b: b.size)。另一种方法是为块添加__gt__ 方法。

    【讨论】:

    • 我试过max(shelf, key=lambda b: b.size),但它引发了另一个TypeError:File "&lt;pyshell#171&gt;", line 5, in insert_animate for i in range(high): TypeError: 'Block' object cannot be interpreted as an integer。我认为这可以归因于我之前为insert_animate 定义的函数。有关如何消除这些错误的任何指导?谢谢!
    • @E.Elle max(shelf, key=lambda b: b.size) 返回一个块。所以基本上你需要range(high.size) 如果那是你想要的
    猜你喜欢
    • 2020-09-07
    • 2021-11-21
    • 1970-01-01
    • 2017-09-14
    • 2017-09-28
    • 2018-01-10
    • 2018-06-03
    • 2018-09-03
    • 2018-05-22
    相关资源
    最近更新 更多