将函数的返回传递给python

时间:2016-10-08 18:38:29

标签: python pygame

我绝对忽略了一些基本的东西......我试图将函数(列表列表)的返回传递给另一个函数作为参数。当我编译时,我得到了没有定义gameGridWords变量的错误。

非常感谢任何帮助!

这是返回gameGridWords的函数:

# 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

这是生成错误的函数:

# 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()  

我在我的gameLoop()函数中调用gridWords()在我的实际while循环之外。

我在while循环中的gameLoop()中调用drawGrid():drawGrid(gameGridWords,True)我得到的错误如下:

回溯(最近一次调用最后一次):文件" C:\ Users \ Mileta Nikoletic \ OneDrive \ Eclipse工作区\ Python \ P-UPPGIFT DD1316 \ Memory \ test6.py",第440行,在gameIntro中()

文件" C:\ Users \ Mileta Nikoletic \ OneDrive \ Eclipse工作区\ Python \ P-UPPGIFT DD1316 \ Memory \ test6.py",第192行,在gameIntro按钮中(" Play(空格键)",150,400,250,50,灰色,蓝色,动作="播放")

文件" C:\ Users \ Mileta Nikoletic \ OneDrive \ Eclipse工作区\ Python \ P-UPPGIFT DD1316 \ Memory \ test6.py",第154行,按钮gameLoop()

文件" C:\ Users \ Mileta Nikoletic \ OneDrive \ Eclipse工作区\ Python \ P-UPPGIFT DD1316 \ Memory \ test6.py",第423行,在gameLoop中

drawGrid(gameGridWords,True) NameError:name' gameGridWords'未定义

如果我将gameGridWords传递给drawGrid,那么我认为gameGridWords会被定义吗?

感谢您的耐心等待!

1 个答案:

答案 0 :(得分:0)

您必须将第一个函数返回的数据分配给变量,即

result = gridWords()

以后使用这个数据和第二个函数。

drawGrid(result)

(顺便说一句,名字result没关系,你可以使用不同的名字)

如果第二个函数不在同一个函数中但在子函数中,则必须将result发送到此子函数,并且此子函数必须将其发送到您的函数

result = gridWords()
subfunction(result)

def subfunction(data):
    drawGrid(data)