2013-04-05 108 views
-1

我需要添加到第二個def語句才能工作?嵌套defs不工作

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

爲了讓任何人能夠幫助你,你需要描述你的具體問題。這段代碼引用了很多未定義的函數。 – 2013-04-05 03:34:51

+0

在一個不相關的說明中,您可能想給[PEP8](http://www.python.org/dev/peps/pep-0008/),Python的風格指南,閱讀。 – 2013-04-05 03:35:22

+0

你定義了removeColor,但你永遠不會調用該函數。 – furins 2013-04-05 14:31:17

回答

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) 
+0

@furins ..感謝您的帖子。我還有一個問題。爲什麼RemoveColor(圖片,顏色)不起作用,程序會通過「for」語句,但它會將所有顏色都去掉並重新繪製成黑色。我如何才能去除只需要的顏色? – EsJe 2013-04-05 17:36:13

+0

@EsJe請讓我知道你正在使用的庫,我會試着回答...什麼庫/模塊定義'getPixel'? – furins 2013-04-05 18:58:48

+0

@fusins我不明白你是什麼意思的模塊或庫,因爲我是新手編程。你認爲你可以更具體一點嗎? – EsJe 2013-04-05 21:29:46