游戏循环中函数输出的python范围不评估

Posted

技术标签:

【中文标题】游戏循环中函数输出的python范围不评估【英文标题】:python Scopage of a function output within gameloop not evaluating 【发布时间】:2018-06-15 17:18:21 【问题描述】:

我的问题是,在gameloop中调用函数check_win后, 即使函数评估为假(我已经检查过,所以这是 情况下),我将此错误值分配给循环内的gamePlaying, 但是游戏循环gamePlaying 条件仍然评估为真 并继续前进。

def check_win(): #function checks for three in a row across, down, and diagonals
        for x in range (0, 3) :
            y = x*3
            if (board[y] == board[(y + 1)] and board[y] == board[(y + 2)]):
                gamePlaying = False
            else:
                gamePlaying = True
            if (board[x] == board[(x + 3)] and board[x] == board[(x + 6)]):
                gamePlaying = False
            else:
                gamePlaying = True
            if((board[0] == board[4] and board[0] == board[8]) or 
                (board[2] == board[4] and board[4] == board[6])):
                gamePlaying = False
            else:
                gamePlaying = True
        return(gamePlaying)

    currentPlayer = [first_player,second_player] #creates list to iterate over turns
    gamePlaying = True #bool for gameloop

    while gamePlaying: #main game loop
        for i in currentPlayer: #iterates over current player to switch turns

            draw_board()
            place_move = int(input(first_move + ' what is your move? ')) #inputs then places move for first_player
            board[place_move] = first_player
            gamePlaying = check_win() #should take bool output of check_win and store to cont or end gameloop

            draw_board()
            place_move = int(input(second_move + ' what is your move? ')) #inputs then places move for second_player
            board[place_move] = second_player
            gamePlaying = Check_win() #should take bool output of check_win and store to cont or end gameloop

【问题讨论】:

其他check win不应该大写但还是不行 【参考方案1】:

问题在于,当您打算使用 elif 时,您使用的是 if 语句。请参阅docs。

但是,您可能想要做的是 return False 在这些点上,因为这将允许函数提前退出,您将不得不担心 gamePlaying 会被稍后的 if 语句设置回 True .

【讨论】:

以上是关于游戏循环中函数输出的python范围不评估的主要内容,如果未能解决你的问题,请参考以下文章

引用函数做游戏

python二次学习之一(变量,判断语句,循环,函数,导入*.py)

python机器学习classification_report()函数 输出模型评估报告

使用for循环来定义多个函数 - Python

Python函数式编程,范围和变量。我哪里错了?

python输出0到100的偶数?