passing the return of a function into another function in python
<p>I am absolutely overlooking something basic here... I'm trying to pass the return of a function (a list of lists) into another function as an argument. When i compile i get the error that the gameGridWords variable is not defined. </p>

<p>Any help would be greatly appreciated!</p>

<p>This is the function that returns gameGridWords:</p>

<pre><code># Populates the tile grid with random word pairs
###########################################################################    
def gridWords():    
    # Picks 18 random words from text file to a list, doubles list and shuffles it, returns a list with 18 pairs of words
    words = [line.strip() for line in open("words.txt", 'r')]      
    randomWords=[]             
    for i in range(0, 18):
        element=random.randint(0, len(words)-1)
        randomWords.append(words[element])    
    randomWords.extend(randomWords)
    randomWordPairs=randomWords
    random.shuffle(randomWordPairs)

    # Populates the game grid with word pairs
    gameGridWords=[]
    for row in range(6):
        columnls=[]
        for column in range(6):
            columnls.append(randomWordPairs[0])
            del randomWordPairs[0] 
        gameGridWords.append(columnls)
    #print (gameGridWords)
    return gameGridWords
</code></pre>

<p>This is the function that generates the error:</p>

<pre><code># Draws the game grid and fills it with 18 pairs of random words
###########################################################################  
def drawGrid(gameGridWords, revealed=True):

    # Background color
    gameDisplay.fill(BLACK) 
    # Draws the grid with gray tiles and populates it with random word pairs
    for row in range(6):
            for column in range(6):
                if not revealed:
                    color = GRAY
                    pygame.draw.rect(gameDisplay,color,[(MARGIN + WIDTH) * column + MARGIN,(MARGIN + HEIGHT) * row + MARGIN,WIDTH,HEIGHT])

                else:
                    word_display(gameGridWords[row][column], BLACK, row, column)
    """
    # Fills tiles with blue 
    for row in range(6):
        for column in range(6):
            if grid[row][column] == 1:
                color = BLUE
                pygame.draw.rect(gameDisplay,color,[(MARGIN + WIDTH) * column + MARGIN,(MARGIN + HEIGHT) * row + MARGIN,WIDTH,HEIGHT])

    """

    timer()      
    tryCounter()
    #pygame.display.update()  
</code></pre>

<p>I call gridWords() inside my gameLoop() function outside my actual while loop.</p>

<p>I call drawGrid() in my gameLoop() inside the while loop with : drawGrid(gameGridWords, True) The error i get is as follows:</p>

<p>Traceback (most recent call last): File "C:\Users\Mileta Nikoletic\OneDrive\Eclipse workspace\Python\P-UPPGIFT DD1316\Memory\test6.py", line 440, in gameIntro()</p>

<p>File "C:\Users\Mileta Nikoletic\OneDrive\Eclipse workspace\Python\P-UPPGIFT DD1316\Memory\test6.py", line 192, in gameIntro button("Play (space bar)", 150,400,250,50, GRAY, BLUE, action="play")</p>

<p>File "C:\Users\Mileta Nikoletic\OneDrive\Eclipse workspace\Python\P-UPPGIFT DD1316\Memory\test6.py", line 154, in button gameLoop()</p>

<p>File "C:\Users\Mileta Nikoletic\OneDrive\Eclipse workspace\Python\P-UPPGIFT DD1316\Memory\test6.py", line 423, in gameLoop</p>

<p>drawGrid(gameGridWords, True)
NameError: name 'gameGridWords' is not defined</p>

<p>If i passed gameGridWords into drawGrid, than i assume that gameGridWords would be defined? </p>

<p>Thanks for your patience!</p>

<p>You have to assign data returned by first function to variable ie.</p>

<pre><code>result = gridWords()
</code></pre>

<p>and later use this data with second function.</p>

<pre><code>drawGrid(result)
</code></pre>

<p>(btw. name <code>result</code> doesn't matter, you can use different name)</p>

<p>if second function isn't in the same function but in subfunction then you have to send <code>result</code> to this subfunction and this subfunction has to send it to your function</p>

<pre><code>result = gridWords()
subfunction(result)

def subfunction(data):
    drawGrid(data)
</code></pre>

python pygame