Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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 如何使用另一个函数终止函数中的while循环。(井字游戏)_Python_Function_Loops_While Loop_Tic Tac Toe - Fatal编程技术网

Python 如何使用另一个函数终止函数中的while循环。(井字游戏)

Python 如何使用另一个函数终止函数中的while循环。(井字游戏),python,function,loops,while-loop,tic-tac-toe,Python,Function,Loops,While Loop,Tic Tac Toe,我正在用Python编写一个Tic-Tac-Toe程序。我已经完成了打印棋盘、接受用户输入和实际玩游戏的功能(棋盘随着每次输入而变化)。但是,我似乎无法在mainexec()函数中运行输出(gboard)函数来终止,而True:循环。代码如下: def board(gboard): print("\n"*10) print(gboard[7]+'|'+gboard[8]+'|'+gboard[9]) print(gboard[4]+'|'+gboard[5]+'|'+gb

我正在用Python编写一个Tic-Tac-Toe程序。我已经完成了打印棋盘、接受用户输入和实际玩游戏的功能(棋盘随着每次输入而变化)。但是,我似乎无法在main
exec()
函数中运行
输出(gboard)
函数来终止
,而True:
循环。代码如下:

def board(gboard):
    print("\n"*10)
    print(gboard[7]+'|'+gboard[8]+'|'+gboard[9])
    print(gboard[4]+'|'+gboard[5]+'|'+gboard[6])
    print(gboard[1]+'|'+gboard[2]+'|'+gboard[3])


def outcome(gboard):
    global ini
    if(gboard[1] == 'X') and (gboard[2] == 'X') and gboard[3] == 'X':
        print('Winner is X!')
        ini = 0
        return ini



def exec():
    move_counter = 0
    ini = 1
    winner = 'X'
    outcomes = ['X','O']

    print("Welcome to Tic Tac Toe!")
    print("Here's how the game works:\nEach number on the keypad represents a place on the table.")
    print("For example the number 1 stands for the box in column 1 row 3, while number 6 stands for column 3 row 2.")
    print("You will tell the computer where you wish to play using this system!")
    print("\n"*2+"Here is a visual representation of the board and the key numbers: ")
    print('7|8|9')
    print('4|5|6')
    print('1|2|3')
    print('If you wish to play X in the middle, you tell the computer to place X in position 5. Here is how a blank board will evolve:')
    print(' | | ')
    print(' |X| ')
    print(' | | ')
    print("So. Let's play!")

    player2 = None
    player1 = input('Player 1, would you like to be X or O(represents the letter)?: ')
    player1 = player1.upper()
    if(player1 == 'X'):
        player2 = 'O'
    else:
        player2 = 'X'
    print(f"Ok. Player 1 is {player1} and so that means that Player 2 is {player2}")


    gboard = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
    while True:
        move = int(input('Player 1, where would you like to play?: '))
        gboard[move] = player1
        move_counter += 1
        board(gboard)
        outcome(gboard)
        if(ini == 0) or move_counter == 9:
            break

        move2 = int(input('Player 2, where would you like to play?: '))
        gboard[move2] = player2
        move_counter += 1
        board(gboard)
        outcome(gboard)

        if(ini == 0) or move_counter == 9:
            break





    print('DONE!')
exec()
在这段代码中,我只输入了一个赢的条件,如果最后一行都是X-这就是我用来测试程序的。当我运行程序,并用X输入最下面一行时,它会打印“Winner is X!”但不会打破while循环。但是,当move_计数器达到9时,它会中断while循环。执行情况如下:

  Welcome to Tic Tac Toe!
Here's how the game works:
Each number on the keypad represents a place on the table.
For example the number 1 stands for the box in column 1 row 3, while number 6 stands for column 3 row 2.
You will tell the computer where you wish to play using this system!


Here is a visual representation of the board and the key numbers:
7|8|9
4|5|6
1|2|3
If you wish to play X in the middle, you tell the computer to place X in position 5. Here is how a blank board will evolve:
 | |
 |X|
 | |
So. Let's play!
Player 1, would you like to be X or O(represents the letter)?: X
Ok. Player 1 is X and so that means that Player 2 is O
Player 1, where would you like to play?: 1
 | |
 | |
X| |
Player 2, where would you like to play?: 4
 | |
O| |
X| |
Player 1, where would you like to play?: 2
 | |
O| |
X|X|
Player 2, where would you like to play?: 5
 | |
O|O|
X|X|
Player 1, where would you like to play?: 3
 | |
O|O|
X|X|X
Winner is X!
Player 2, where would you like to play?:

我不希望程序会问玩家2,在决定胜利者后他们想在哪里比赛。为什么我的结果函数没有跳出while循环?

似乎
exec
函数中的
ini
变量与
output
函数中的
ini
变量不同(一个是局部变量,一个是全局变量)。只需在
exec
函数中将
ini
声明为全局变量,即可解决此问题,如下所示:

def exec():
    move_counter = 0
    global ini <------- here
    ini = 1
    winner = 'X'
    outcomes = ['X','O']
def exec():
移动计数器=0
全局ini您在exec()ini=1中设置,因此,即使通过获胜将其设置为0,它也将再次设置为1。
您可以从exec中删除ini=1,并在所有方法之前声明它,以便在游戏开始时它的值为1,在游戏结束时它的值仅设置为0

为什么我的结果函数没有打破while循环

因为
ini
仍然等于1。主函数有一个名为
ini
的局部变量,因此即使从
output
设置全局
ini
,它也将被忽略

您不应该为此使用全局变量。您对
返回ini的想法是正确的。但是,调用代码并不关心这个名称-它只关心值。当您调用
outcome
且X获胜时,它将
返回
0
的值。您应该分配以下内容:

ini = outcome(gboard)

下一步是,
outcome
需要返回一个值,即使X没有赢。(当然,你应该测试X可以赢,O可以赢的其他方式。)你可以使用多个不同的值来指示谁赢,或者游戏是否应该继续。

你应该将你的
ini
var作为全局变量

尝试将其放在方法范围之外的开始处,并在其中导入
全局ini



ini=None#一旦宣布了这一轮的获胜者,您的目标是立即关闭程序吗?“exec”中的“ini”是此函数的本地值。顺便说一下:“exec”是标准库中的一个函数。你应该用另一个名字,没错。谢谢!
ini = None # <--- declare ini as global without value  

def board(gboard):
    print("\n"*10)
    print(gboard[7]+'|'+gboard[8]+'|'+gboard[9])
    print(gboard[4]+'|'+gboard[5]+'|'+gboard[6])
    print(gboard[1]+'|'+gboard[2]+'|'+gboard[3])


def outcome(gboard):
    global ini
    if(gboard[1] == 'X') and (gboard[2] == 'X') and gboard[3] == 'X':
        print('Winner is X!')
        ini = 0
        return ini



def exec():
    global ini # <--- import ini global in this scope
    move_counter = 0
    ini = 1
    winner = 'X'
    outcomes = ['X','O']

    print("Welcome to Tic Tac Toe!")
    print("Here's how the game works:\nEach number on the keypad represents a place on the table.")
    print("For example the number 1 stands for the box in column 1 row 3, while number 6 stands for column 3 row 2.")
    print("You will tell the computer where you wish to play using this system!")
    print("\n"*2+"Here is a visual representation of the board and the key numbers: ")
    print('7|8|9')
    print('4|5|6')
    print('1|2|3')
    print('If you wish to play X in the middle, you tell the computer to place X in position 5. Here is how a blank board will evolve:')
    print(' | | ')
    print(' |X| ')
    print(' | | ')
    print("So. Let's play!")

    player2 = None
    player1 = input('Player 1, would you like to be X or O(represents the letter)?: ')
    player1 = player1.upper()
    if(player1 == 'X'):
        player2 = 'O'
    else:
        player2 = 'X'
    print(f"Ok. Player 1 is {player1} and so that means that Player 2 is {player2}")


    gboard = ['#',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ']
    while True:
        move = int(input('Player 1, where would you like to play?: '))
        gboard[move] = player1
        move_counter += 1
        board(gboard)
        outcome(gboard)
        if(ini == 0) or move_counter == 9:
            break

        move2 = int(input('Player 2, where would you like to play?: '))
        gboard[move2] = player2
        move_counter += 1
        board(gboard)
        outcome(gboard)

        if(ini == 0) or move_counter == 9:
            break





    print('DONE!`enter code here`')
exec()