Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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 函数不';t在while循环之后执行_Python - Fatal编程技术网

Python 函数不';t在while循环之后执行

Python 函数不';t在while循环之后执行,python,Python,我是python新手。我正在尝试创建一个“tic-tac-toe”游戏 但只是遇到了一些麻烦 名为“handle turn”的函数的输入在要求用户输入时不执行任何操作 基本上,它发生在我添加“While”循环之后。请告诉我我做错了什么。如何在下面的代码中执行/请求用户输入 def handle_turn(): position = input("Please enter a number from 1-9: ") position = int(position)

我是python新手。我正在尝试创建一个“tic-tac-toe”游戏 但只是遇到了一些麻烦

名为“handle turn”的函数的输入在要求用户输入时不执行任何操作

基本上,它发生在我添加“While”循环之后。请告诉我我做错了什么。如何在下面的代码中执行/请求用户输入

def handle_turn():
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = "x"
        print(display_board())
根本没有“错误”,它只是不执行请求用户输入的def handle_turn()函数。 我将在下面附上完整的代码

#board
board = ["-", "-", "-",
         "-", "-", "-",
         "-", "-", "-"]

# if game is still going
game_is_going = True

#who win
winner = None

#current_player
current_player = "x"

#board display
def display_board():
    print(board[0] + "|" + board[1] + "|" + board[2])
    print(board[3] + "|" + board[4] + "|" + board[5])
    print(board[6] + "|" + board[7] + "|" + board[8])
    return

def play_game():
        return

print(display_board())

# action while game is still going
while game_is_going:

    #handle_turn(current_player)

    #check_if_game_over()

    #flip_player()

    if winner == "x" or winner == "o":
        print(winner + " won")


def handle_turn():
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = "x"
        print(display_board())

print(handle_turn())

def check_if_game_over():
    check_if_win()
    check_if_tie()

def check_if_win():
    #check rows
    #check columns
    #check diagonals
    return

def flip_player():
    return


play_game()
你草签

game_is_going = True 

在代码的一开始,使用while循环检查它是否为真。您的代码将永远不会经过while循环,直到它为false,这将永远不会发生。

在使用while循环之前,请先定义在while循环中调用的函数,然后再进行尝试

#板
董事会=[“-”、“-”、“-”,
"-", "-", "-",
"-", "-", "-"]
#如果游戏还在继续
游戏进行中=正确
#谁赢了
赢家=无
#当前游戏玩家
当前_player=“x”
#板显示
def显示板():
打印(板[0]+“|”+板[1]+“|”+板[2])
打印(板[3]+“|”+板[4]+“|”+板[5])
打印(板[6]+“|”+板[7]+“|”+板[8])
返回
def play_game():
返回
打印(显示板())
def手柄转动()
位置=输入(“请输入1-9之间的数字:”)
位置=int(位置)-1
板[位置]=“x”
打印(显示板())
打印(手柄转动()
def检查游戏是否结束()
检查您是否成功()
检查是否连接()
def检查是否成功()
#检查行
#检查列
#检查对角线
返回
def flip_播放器():
返回
#趁游戏还在进行时采取行动
当游戏进行时:
手柄旋转(当前玩家)
检查游戏是否结束()
flip_播放器()
如果获胜者==“x”或获胜者==“o”:
打印(获胜者+获胜者)
玩游戏

首先,您已经在程序的最开始处初始化了
游戏正在进行=True
。现在,当执行while循环时,
游戏正在进行
永远不会出错,因此它将成为一个无限循环。这就解释了为什么不询问输入,也没有生成错误。您需要在Python中很好地处理缩进

现在,您的代码将变成这样:-

board = ["-", "-", "-",
         "-", "-", "-",
         "-", "-", "-"]
game_is_going = True
winner = None
current_player = "x"
def display_board():
    print(board[0] + "|" + board[1] + "|" + board[2])
    print(board[3] + "|" + board[4] + "|" + board[5])
    print(board[6] + "|" + board[7] + "|" + board[8])
print(display_board())
def handle_turn():
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = current_player
        print(display_board())
def play_game():
    while game_is_going:
        handle_turn()
        if check_if_game_over:
            game_is_going = False
        flip_player()
def check_if_game_over():
    check_if_win()
    check_if_tie()           # Print in the function, that the game was a tie
    # Also keep a track if all the 9 blocks have been filled and there is no space left for further moves
def check_if_win():
    # Check who is the winner and do winner = "x" or winner = "o" as required
    return
def flip_player():
    if current_player == "x":
        current_player = "o"
    else:
        current_player = "x"
play_game()
if winner == "x" or winner == "o":
    print(winner + " won")

我猜你的方法安排是不正确的,因为你没有定义任何类,所以它误导了编译器

并且您没有将参数传递给handle_turn():方法

下面是您的工作代码

board = ["-", "-", "-",
         "-", "-", "-",
         "-", "-", "-"]

# if game is still going
game_is_going = True

#who win
winner = None

#current_player
current_player = "x"

#board display
def display_board():
    print(board[0] + "|" + board[1] + "|" + board[2])
    print(board[3] + "|" + board[4] + "|" + board[5])
    print(board[6] + "|" + board[7] + "|" + board[8])
    return

def play_game():
        return

print(display_board())

def handle_turn(current_player):
        position = input("Please enter a number from 1-9: ")
        position = int(position) - 1

        board[position] = current_player

        print(display_board())

def check_if_game_over():
    check_if_win()
    # check_if_tie()


def check_if_win():
    #check rows
    #check columns
    #check diagonals
    return
# action while game is still going


def flip_player():
    return

while game_is_going:

    handle_turn(current_player)

    check_if_game_over()

    flip_player()

    if winner == "x" or winner == "o":
        print(winner + " won")



print(handle_turn())

1.修正你的缩进。它在python中很重要,而且您现在拥有的一些东西可能与您的源代码不匹配。2.尝试在while循环之后添加一个print语句-我怀疑它正在无限运行。非常感谢,我添加了“break”,这就解决了它。非常感谢。请在提供答案之前完整阅读张贴的问题。问题在于在循环内执行函数。函数在引入循环之前工作,但在添加循环之后不工作。让函数在循环中注释掉。。。二,。在循环的下面,他们再次调用它,我想这就是他们正在询问的。由于循环,第二种情况永远不会发生。