def get_position (row_index,col_index,size):
""" (int,int,int) -> int
Precondition:size >=col_index and size >= row_index
Return the str_index of the cell with row_index,col_index and size
>>>get_position (2,2,4)
5
>>>get_position (3,4,5)
13
"""
str_index = (row_index - 1) * size + col_index - 1
return str_index
def make_move( symbol,row_index,col_index,game_board):
"""(str,int,int,str) -> str
Return the resultant game board with symbol,row_index,col_index and game_board
>>>make_move("o",1,1,"----")
"o---"
>>>make_move("x",2,3,"---------")
"-----x---"
"""
length=len(game_board)
return "-"*(str_index-1)+symbol+"-"*(length-str_index)
当我评估它时,显示
“NameError:名称'str_index'未定义”
我想我已经定义了str-index。
答案 0 :(得分:0)
您在str_index
功能中使用make_move
。该变量在函数范围内不存在。
def make_move(symbol,row_index,col_index,game_board):
"""(str,int,int,str) -> str
Return the resultant game board with symbol,row_index,col_index and game_board
>>>make_move("o",1,1,"----")
"o---"
>>>make_move("x",2,3,"---------")
"-----x---"
"""
length=len(game_board)
# I am assuming length is the size for get_position
str_index = get_position(row_index, col_index, length)
return "-"*(str_index-1)+symbol+"-"*(length-str_index)
答案 1 :(得分:0)
尝试将代码更改为:
return (row_index - 1) *size + col_index - 1
和
str_index =get_position(row_index, col_index, size)
return "-"*(str_index-1)+symbol+"-"*(length-str_index)
注意:您需要在函数中添加size
参数或进行计算。