Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套def不工作_Python - Fatal编程技术网

Python 嵌套def不工作

Python 嵌套def不工作,python,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 co

我需要向第二个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)

这里,我从
main
函数中取出
RemoveColor
函数,并从
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)

为了让任何人都能帮助你,你需要描述你的具体问题。此代码段引用了许多未定义的函数。请注意,您可能需要阅读《Python样式指南》。您定义了removeColor,但从未调用该函数。@furins。。谢谢你的来信。我还有一个问题。为什么RemoveColor(pic,color)不工作,程序会执行“for”语句,但它会删除所有颜色并重新绘制为黑色。我如何让它只删除所请求的颜色?@EsJe请告诉我您使用的是哪个库,我将尝试回答。。。什么库/模块定义了
getPixel
?@fusins我不明白模块或库是什么意思,因为我是编程新手。你认为你可以更具体一点吗?在你给我们看的代码之前,你是否写过“导入[某些模块]”?你可以发布你的全部代码吗?但是,这是与“嵌套定义”问题无关的第二个问题:您应该在stackoverflow中开始一个新问题,以便第一眼就得到社区的帮助,但是,我发现在removecolor函数中,您从未检查for循环中的像素是否具有所需的颜色,您只需替换所有像素,而根本不使用颜色属性。