board = range(1,26) #the gameboardfor row in [board[i:i+5] for i in range(0,22,5)]: #go over chunks of five print('|'.join(["{:<2}".format(n) for n in row])+"|") #justify each number, join by | print("-"*15) #print the -'s产生>>> 1 |2 |3 |4 |5 |---------------6 |7 |8 |9 |10|---------------11|12|13|14|15|---------------16|17|18|19|20|---------------21|22|23|24|25|---------------或使用grouper@abarnert建议的配方:for row in grouper(5, board):
只是为了好玩,这里有一个1-liner来创建编号的行:['|'.join([str(y).center(4) for y in x]) for x in map(None,*[reversed(range(1,26))]*5)]稍微分解一下,添加行,仍然不是一个干净的答案:nums = map(None,*[reversed(range(1,26))]*5)rows = ['|'.join([str(y).center(4) for y in x]) for x in nums]board = ('\n'+'-'*len(rows[0])+'\n').join(rows)print board