Michael的Python笔记(一)

来源:互联网 发布:巨牌一搜网络一搜同志 编辑:程序博客网 时间:2024/06/08 08:12

Custom Print

Now we can see the contents of our list, but clearly it would be easier to play the game if we could print the board out like a grid with each row on its own line.

We can use the fact that our board is a list of lists to help us do this. Let’s set up a for loop to go through each of the elements in the outer list (each of which is a row of our board) and print them.
说明

First, delete your existing print statement.Then, define a function named print_board with a single argument, board.Inside the function, write a for loop to iterates through each row in board and print it to the screen.Call your function with board to make sure it works.

编写源码:

思路

        构建函数,利用for做循环

board = []
for i in range(1,6):

n = [“O”]
board.append(n*5)

def print_board(board):

for row in board:
print row

print board

0 0