嵌套的defs不起作用

时间:2013-04-05 03:07:02

标签: python

我需要在第二个def语句中添加什么才能使其正常工作?

def main():
    # the user has to choose a picture and then he is asked to
    pic = makePicture(pickAFile())

    # the user is asked to select a color that he wants to remove from a picture
    color = requestString("Which color would you like to remove?")
    show(pic)

    # whats wrong with this last part that doesn't make the modifications to the picture
    def RemoveColor(pic, color):
        r = red
        g = green
        b = blue
        for px in getPixels(pic):
            setRed(px, 0)

        for px in getPixels(pic):
            setGreen(px, 0)

        for px in getPixels(pic):
            setBlue(px, 0)

    repaint(pic)

1 个答案:

答案 0 :(得分:0)

在这里,我从您的RemoveColor函数中取出main函数,我从main内部调用它

def RemoveColor(pic, color):
    r = red
    g = green
    b = blue
    for px in getPixels(pic):
        setRed(px, 0)

    for px in getPixels(pic):
        setGreen(px, 0)

    for px in getPixels(pic):
        setBlue(px, 0)

def main():
    # the user has to choose a picture and then he is asked to
    pic = makePicture(pickAFile())

    # the user is asked to select a color that he wants to remove from a picture
    color = requestString("Which color would you like to remove?")
    show(pic)

    RemoveColor(pic, color) # HERE I'M CALLING RemoveColor
    repaint(pic)