2015-10-03 4 views
0
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: name 'str_index' is not defined"

Я думаю, что я уже определяю СИЛЫ-индекс.

ответ

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) 
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 к функции или вычислить его.

Смежные вопросы