在其中调用函数

时间:2016-11-06 20:02:53

标签: python function

def move(list, wins_1, wins_2):
    global turn
    if turn % 2 == 0:
        sign = "| x "
    else:
        sign = "| o "
y_1 = int(input("Type the value of y: "))
x_1 = int(input("Type the value of x: "))

if list[y_1 - 1][x_1 - 1] == "| x " or list[y_1 - 1][x_1 - 1] == "| o ":
    print("The place is already filled by %s |" % list[y_1 - 1][x_1 - 1])
    move(list, wins_1, wins_2)
else:
    list[y_1 - 1][x_1 - 1] = sign

print_board(list)  #

wins_1, wins_2 = check_winner(sign, wins_1, wins_2)

turn += 1
return wins_1, wins_2

如果用户输入列表的[x] [y]并且已经通过ax或o(它是一个tic tac toe游戏),它应该打印(“这个地方已经填满”)然后让用户输入另一组x和y来放置他的x / o。

这让我想到,也许我可以简单地调用该函数并使其重复。它工作正常,没有错误。 但出于某种原因,它多次打印我的纸板(或多次我已按下“x”和“y”为已填充的地方多次)。

有人可以解释当您在函数中调用函数时会发生什么。我的代码究竟发生了什么?

注意:我的代码很长,这只是一点点。如果需要更多代码,请告诉我。

以下是输出示例:请注意,在将x / o放入已填充的位置后,它会将板打印2次。

Type the name of player 1: 1
type the name of player 2: 1
____________________________________________________________
player_1: 1       X        wins: 0
player_2: 1       O        wins: 0
____________________________________________________________
Type the value of y: 1
Type the value of x: 1
 -------------
 | x |   |   |   
 -------------
 |   |   |   |   
 -------------
 |   |   |   |   
 -------------
____________________________________________________________
player_1: 1       X        wins: 0
player_2: 1       O        wins: 0
____________________________________________________________
Type the value of y: 1
Type the value of x: 1
The place is already filled by | x  |
Type the value of y: 1
Type the value of x: 2
 -------------
 | x | o |   |   
 -------------
 |   |   |   |   
 -------------
 |   |   |   |   
 -------------
 -------------
 | x | o |   |   
 -------------
 |   |   |   |   
 -------------
 |   |   |   |   
 -------------
____________________________________________________________
player_1: 1       X        wins: 0
player_2: 1       O        wins: 0
____________________________________________________________
Type the value of y: 

整个代码:

wins_1 = 0
wins_2 = 0
turn = 0
board = [
        ['|   ', '|   ', '|   ', '|   '],
        ['|   ', '|   ', '|   ', '|   '],
        ['|   ', '|   ', '|   ', '|   ']
    ]
def print_board(board_list):

    for i in range(len(board_list)):
        print(" -------------\n %s" % "".join(board_list[i]))  # .join binder 2 items sammen.
    print(" -------------")

player_1 = input("Type the name of player 1: ")
player_2 = input("type the name of player 2: ")
def game_info(player_1, player_2, wins_1, wins_2):  # prints layout
    print("_" * 60)
    print("player_1: %s       X        wins: %s"
          "\nplayer_2: %s       O        wins: %s"
          % (str(player_1), str(wins_1), str(player_2), str(wins_2)))
   print("_" * 60)
# ovenstående viser output af wins og navn



def print_winner(sign, wins_1, wins_2):

    if sign == "| x ":
        print("%s got 3 in a row, %s wins!" % (player_1, player_1))
        wins_1 += 1
    else:
        print("%s got 3 in a row, %s wins!" % (player_2, player_2))
        wins_2 += 1
    return wins_1, wins_2


def check_winner(sign, wins_1, wins_2):

    # check vertical:    |
    for x in range(0,3):
        if board[0][x] == sign and board[1][x] == sign and board[2][x] == sign:
            wins_1, wins_2 = print_winner(sign, wins_1, wins_2)

    #  check horizontal: -
    for x in range(0, 3):
        if board[x][0] == sign and board[x][1] == sign and board[x][2] == sign:
            wins_1, wins_2 = print_winner(sign, wins_1, wins_2)

    #  check diagonal:   \
    if board[0][0] == sign and board[1][1] == sign and board[2][2] == sign:
        wins_1, wins_2 = print_winner(sign, wins_1, wins_2)
    elif board[0][2] == sign and board[1][1] == sign and board[2][0] == sign:
        wins_1, wins_2 = print_winner(sign, wins_1, wins_2)

    return wins_1, wins_2


def move(list, wins_1, wins_2):
    global turn
    if turn % 2 == 0:
        sign = "| x "
    else:
        sign = "| o "

    y_1 = int(input("Type the value of y: "))
    x_1 = int(input("Type the value of x: "))

    if list[y_1 - 1][x_1 - 1] == "| x " or list[y_1 - 1][x_1 - 1] == "| o ":
        print("The place is already filled by %s |" % list[y_1 - 1][x_1 - 1])
        move(list, wins_1, wins_2)
    else:
        list[y_1 - 1][x_1 - 1] = sign

    print_board(list)  #

    wins_1, wins_2 = check_winner(sign, wins_1, wins_2)

    turn += 1
    return wins_1, wins_2





while True:
    game_info(player_1, player_2, wins_1, wins_2)
    wins_1, wins_2 = move(board, wins_1, wins_2)  # move() sætter et 'tegn' og returner win1/win2

    if wins_1 or wins_2 == 1:
        break
print("*****************************************************\nGame Over.    \n IT WORKED!")

2 个答案:

答案 0 :(得分:0)

你正在进入一个无限循环。您第一次正在浏览move。然后你第二次开始move。假设第二个move完成,那么您的程序将在第一个调用第二个move的{​​{1}}中从中断处继续。只要if list为真,您就会实例化move的另一个实例。

你想要实现的是recursion一个简单的例子。

def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)

您可以通过向前一个函数定义添加两个print()函数来跟踪函数的工作方式:

def factorial(n):
    print("factorial has been called with n = " + str(n))
    if n == 1:
        return 1
    else:
        res = n * factorial(n-1)
        print("intermediate result for ", n, " * factorial(" ,n-1, "): ",res)
        return res  

print(factorial(5))

输出:

factorial has been called with n = 5
factorial has been called with n = 4
factorial has been called with n = 3
factorial has been called with n = 2
factorial has been called with n = 1
intermediate result for  2  * factorial( 1 ):  2
intermediate result for  3  * factorial( 2 ):  6
intermediate result for  4  * factorial( 3 ):  24
intermediate result for  5  * factorial( 4 ):  120
120

答案 1 :(得分:0)

以下是答案:

def move(list, wins_1, wins_2):
    printed = False
    global turn
    if turn % 2 == 0:
        sign = "| x "
    else:
        sign = "| o "

    y_1 = int(input("Type the value of y: "))
    x_1 = int(input("Type the value of x: "))

    if list[y_1 - 1][x_1 - 1] == "| x " or list[y_1 - 1][x_1 - 1] == "| o ":
        print("The place is already filled by %s |" % list[y_1 - 1][x_1 - 1])
        move(list, wins_1, wins_2)
        printed = True
    else:
        list[y_1 - 1][x_1 - 1] = sign

    if printed == False:
        print_board(list)  

    wins_1, wins_2 = check_winner(sign, wins_1, wins_2)

    turn += 1
    return wins_1, wins_2

我添加了一个名为printed的变量,因此只打印一次

然后,如果程序尚未打印,则只允许您打印程序

希望这有助于:)