棋盘游戏分离线Python

时间:2017-09-20 00:28:53

标签: python python-3.x for-loop format

我对python很新,我目前正在尝试创建一个带边框的游戏板。到目前为止我有:

for row in range(5):
print('-'*50)
for col in range(5):
    print('| {:<5}{:<3}'.format(row, col), end = '')
print()

这给了我:

--------------------------------------------------
| 0    0  | 0    1  | 0    2  | 0    3  | 0    4  
--------------------------------------------------
| 1    0  | 1    1  | 1    2  | 1    3  | 1    4  
--------------------------------------------------
| 2    0  | 2    1  | 2    2  | 2    3  | 2    4  
--------------------------------------------------
| 3    0  | 3    1  | 3    2  | 3    3  | 3    4  
--------------------------------------------------
| 4    0  | 4    1  | 4    2  | 4    3  | 4    4  

我的问题是行的末尾没有一行,最后一行下面没有虚线。当我输入end ='|'我最后得到一条线,但中间还有双线,如:

--------------------------------------------------
| 0    0  || 0    1  || 0    2  || 0    3  || 0    4  |
--------------------------------------------------
| 1    0  || 1    1  || 1    2  || 1    3  || 1    4  |
--------------------------------------------------
| 2    0  || 2    1  || 2    2  || 2    3  || 2    4  |
--------------------------------------------------
| 3    0  || 3    1  || 3    2  || 3    3  || 3    4  |
--------------------------------------------------
| 4    0  || 4    1  || 4    2  || 4    3  || 4    4  |

我不知道如何刊登最后一条虚线。

我需要做些什么才能在两端和中间获得一条线? 如何在最后一行下面加一条虚线? 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码实现此特定格式:

@Component({
  selector: 'my-app',
  template: `
    <ul>
      <li *ngFor="let item of collection;trackBy: trackByFn">{{item.id}}</li>
    </ul>
    <button (click)="getItems()">Refresh items</button>
  `,
})
export class App {

  constructor() {
    this.collection = [{id: 1}, {id: 2}, {id: 3}];
  }

  getItems() {
    this.collection = this.getItemsFromServer();
  }

  getItemsFromServer() {
    return [{id: 1}, {id: 2}, {id: 3}, {id: 4}];
  }

  trackByFn(index, item) {
    return index; // or item.id
  }
}

输出:

*ngFor="let item of collection; trackBy:index"

*ngFor="let item of collection; let i = index; trackBy:i"