简体   繁体   English

函数在while循环后不执行

[英]Function doesn't executes after while loop

I am new in python.我是python的新手。 I am trying to create a "tic-tac toe" game but just ran into some troubles.我正在尝试创建一个“井字游戏”游戏,但遇到了一些麻烦。

The input of the function called "handle turn" doesn't executes anything while it supposed to ask user for input.名为“handle turn”的函数的输入在它应该要求用户输入时不执行任何操作。

Basically it happened after I added "While" loop.基本上它发生在我添加“While”循环之后。 Please tell me what I'm doing wrong.请告诉我我做错了什么。 How do I execute/ask user for input in code below我如何在下面的代码中执行/要求用户输入

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

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

There is NO "ERROR" at all, it just doesn't executes def handle_turn() function which ask user for input.根本没有“错误”,它只是不执行要求用户输入的 def handle_turn() 函数。 I will attach entire code below.我将在下面附上完整的代码。

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

Well try out by defining the functions you call inside the while before using the while loop.在使用 while 循环之前,通过定义您在 while 中调用的函数来尝试一下。

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

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

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


play_game()

Firstly, you have initialized game-is-going = True at the very beginning of the program.首先,您在程序一开始就初始化了game-is-going = True Now when while loop is getting executed, game_is_going is never getting false, and hence it is becoming an infinite loop.现在当 while 循环被执行时, game_is_going永远不会game_is_going ,因此它变成了一个无限循环。 This explains why the input is not asked, and also there is no error generated.这解释了为什么不要求输入,也没有产生错误。 You need to take care of the indentation very well in Python.您需要在 Python 中很好地处理缩进。

Now, your code will become something like this :-现在,您的代码将变成这样:-

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")

I guess Your arrangement of method is incorrect because you didn't defined any class so it misguiding the compiler我猜你的方法安排不正确,因为你没有定义任何类,所以它误导了编译器

and You are not passing the param to handle_turn(): method并且您没有将参数传递给 handle_turn(): 方法

below is your working code下面是你的工作代码

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

You initialise你初始化

game_is_going = True 

at the very start of your code, then check whether it is True with a while loop.在代码的最开始,然后使用 while 循环检查它是否为 True。 Your code will never progress past the while loop until it is false, which will never happen.您的代码永远不会超过 while 循环,直到它为 false 为止,这永远不会发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM