#/usr/bin/python # -*- coding:utf-8 -*-
# width 单个网格有多少个 - 宽度
# height 单个网格有多少个 | 高度
# lateral 横向有多少个网格
# vertical 竖向有多少个网格 def printGrid(width,height,lateral,vertical): for i in range(vertical): print_lateral(lateral,width) for n in range(height): print_vertical(lateral,width) print print_lateral(lateral,width) def print_vertical(lateral,width): for i in range(lateral): print '|', print ' '*width, # print ' '* (width-1), print ' ', print '|' def print_lateral(lateral,width): for i in range(lateral): print '+', print '-' * width, # print '- ' * (width-1), print '-', print '+' if __name__ == '__main__': printGrid(5,5,3,2)
+ ----- + ----- + ----- + # + - - - - - + - - - - - + - - - - - + | | | | | | | | | | | | | | | | | | | | + ----- + ----- + ----- + | | | | | | | | | | | | | | | | | | | | + ----- + ----- + ----- +
来自 http://www.codingpy.com/books/thinkpython2/03-functions.html 的题目
教程下方给出的答案 :http://greenteapress.com/thinkpython2/code/grid.py 感觉好麻烦