Michael的Python笔记(一)

本文介绍了一种通过Python编程实现的定制打印功能,该功能能够将游戏棋盘以网格的形式展示出来,使得每一行都在单独的一行显示,极大地方便了玩家对棋盘布局的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值