【问题标题】:Turtle graphics animation (shelf)海龟图形动画(架子)
【发布时间】:2017-08-01 23:05:03
【问题描述】:

我想定义一个函数insert_animate(blockposition, shelf, high),以便它从shelf 中删除blockposition 处的块,并将此块插入high 的高位,其中high 是一个整数。该函数应该在零和高位之间找到这个位置,不包括高位。如果没有找到这样的位置,则将块插入到高位置。

我想出了这个功能:

def insert_animate(blockposition, shelf, high):
    if blockposition==0:
        return shelf
    else:
        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

但是,这仅适用于某些情况,而不是全部。我不确定我哪里出错了。

s = shelf.init_shelf((2, 6, 1, 4, 8, 3, 9))
print(insert_animate(0, s, 0)) # returns [Block size: 2, Block size: 6, Block size: 1, Block size: 4, Block size: 8, Block size: 3, Block size: 9]
print(insert_animate(3, s, 3)) 
# returns [Block size: 1, Block size: 2, Block size: 4, Block size: 6, Block size: 4, Block size: 4, Block size: 8, Block size: 3, Block size: 9]

但应该是(第二次打印):

[Block size: 1, Block size: 2, Block size: 4, Block size: 6, Block size: 4, Block size: 4, Block size: 8, Block size: 3, Block size: 9]

print(insert_animate(6, s, 6))
# returns [Block size: 1, Block size: 2, Block size: 4, Block size: 6, Block size: 4, Block size: 4, Block size: 8, Block size: 8, Block size: 8, Block size: 8, Block size: 8, Block size: 8, Block size: 3, Block size: 9]

但应该是:

[Block size: 1, Block size: 2, Block size: 4, Block size: 6, Block size: 8, Block size: 3, Block size: 9]

为什么我的代码适用于某些情况,但不适用于其他情况?问题出在我的 for 循环上吗?如果有人可以提供帮助,我将不胜感激!谢谢!

这是我正在处理的文件:

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 for-loop turtle-graphics


    【解决方案1】:

    在致电insert_animate() 后,很难按照您的起始顺序和最终顺序示例进行操作,但这是我对出了什么问题的猜测:

    我相信您的else 子句缩进不正确(太深),以至于它实际上应该成为for 语句的一部分时却成为了if 语句的一部分:

    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:  # no break
            s.insert(high, a)
    
        return shelf
    

    正如最初编写的那样,else 子句可以执行多次,但您只希望它执行一次,当for 循环通过break 以外的方式退出时。以这种方式绑定到循环的else 子句可以被认为是“不中断”的意思,即如果循环有break 语句但没有被执行,则执行此代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      相关资源
      最近更新 更多