2017-01-24 127 views
-4

我正在尝试为游戏创建一个函数,但是我的身体出现了一些问题。我创建了一个函数,该函数使用函数的参数返回一个字符串。根据我的功能,我需要将board(parameter1)行索引row_index(Parameter2)中的字符作为单个字符串返回。
任何帮助将是有用的。泰
这是我的函数头和身体:打印棋盘排

def make_str_from_row(board, row_index): 
    """ (list of list of str, int) -> str 

    # Return the characters from the row of the board with index row_index 
    as a single string. 

    >>> make_str_from_row([['A', 'N', 'T', 'T'], ['X', 'S', 'O', 'B']], 0) 
    'ANTT' 
    """ 
    for i in range(len(row_index) - 1): 
     if board[i] row_index[i] 
     return board[i] 
+0

它是做什么的那一刻? –

+0

@PeterWood:我的猜测是,由于怪异的if语句,它会抛出语法错误。 –

+0

'row_index'是一个整数吗?你只能在序列上使用'len',比如列表。 'board'是一个列表。 –

回答

0

我真不明白你的目标是通过使用for循环做什么,等

你可以在row_index与简单地读取该行:

board[row_index] 

和人物的结合是可以做到的 - 你猜怎么着 - .join

def make_str_from_row(board, row_index): 
    return ''.join(board[row_index]) 

这里''分隔符:放在字符之间的字符串。由于我们不想在其间插入任何字符,因此它是空字符串。 join将一个字符串迭代作为参数,这里是该行的字符列表并将它们连接在一起。所以,如果你给''.join(..)像字符串列表:

''.join(['Foo','Bar','Qux','Hello','World']) 

则回复:

'FooBarQuxHelloWorld' 
+0

@PaulRooney:谢谢你指出。也许这些手指太习惯荷兰语了。 –

+0

你能教我如何。加入在我的功能pls? –

+0

@KennethRivadeneiraGuadamud:那时答案的哪一部分还不清楚? –