不将值分配给另一个函数的列表

时间:2018-03-09 16:38:46

标签: python-3.x

我已经编写了一些代码来制作非常基本的tic tac toe游戏但是在我的代码中我试图将一个字符串值作为变量(marker)分配给名为display_board的列表返回函数返回一个名为board的列表。

问题:

在此代码中,display_board()[col_guess][row_guess] == marker行在此处不执行任何操作,因为它没有将marker输入变量分配给名为board的列表

守则:

from random import randint
#Step 1: Write a function that can print out a board. Set up your board as a list,
#where each index 1-9 corresponds with a number on a number pad, so you get a 3 by 3 board representation.
def display_board():
    board = []
    for play in range(0,3):
        board.append(["O"]*3)
    for joinBoard in board:
        print(" ".join(joinBoard))
        return(board)


#Step 2: Write a function that can take in a player input and assign their marker as 'X' or 'O'.
#Think about using while loops to continually ask until you get a correct answer.

def player_input():
    print("Type the board in which you want play? 'X' or 'O'")
    marker = str(input(" 'X' or 'O' ")).upper()
    print("You chose {} as a board, now we'll assign it.".format(marker))
    col_random = randint(0,3)
    print(col_random)
    row_random = randint(0,3)
    print(row_random)
    print("\n Now we'll ask you to guess the position of hidden ")
    col_guess= int(input('Guess the colum: >> '))
    row_guess = int(input('Guess the row: >> '))
    while col_guess == col_random and row_guess == row_random:
        display_board()[col_guess][row_guess] == marker
        print(display_board())
        break


player_input()

1 个答案:

答案 0 :(得分:0)

实际上

char[]

此代码

while col_guess == col_random and row_guess == row_random:
    display_board()[col_guess][row_guess] == marker
    print(display_board())
    break

调用函数display_board()[col_guess][row_guess] == marker 并分配你想要的东西,但是

您正在查看

的输出
display_board()

实际上这又是创建新列表,因此以前的列表被破坏了。这样做。

print(display_board())