Python 乙個四行四列的小網格繪製的程式

2021-10-01 21:49:10 字數 4485 閱讀 8250

在看think python 第三章 函式章節時,有個練習是:寫乙個四行四列的小網格繪製的程式。

官方例程為:

上面例程中的**為:

"""this module contains a code example related to

think python, 2nd edition

by allen downey

license:

"""from __future__ import print_function, division

# here is a mostly-straightforward solution to the

# two-by-two version of the grid.

defdo_twice

(f):

f() f(

)def

do_four

(f):

do_twice(f)

do_twice(f)

defprint_beam()

:print

('+ - - - -'

, end=

' ')

defprint_post()

:print

('| '

, end=

' ')

defprint_beams()

: do_twice(print_beam)

print

('+'

)def

print_posts()

: do_twice(print_post)

print

('|'

)def

print_row()

: print_beams(

) do_four(print_posts)

defprint_grid()

: do_twice(print_row)

print_beams(

)print_grid(

)# here is a less-straightforward solution to the

# four-by-four grid

defone_four_one

(f, g, h)

: f(

) do_four(g)

h()def

print_plus()

:print

('+'

, end=

' ')

defprint_dash()

:print

('-'

, end=

' ')

defprint_bar()

:print

('|'

, end=

' ')

defprint_space()

:print

(' '

, end=

' ')

defprint_end()

:print()

defnothing()

:"do nothing"

defprint1beam()

: one_four_one(nothing, print_dash, print_plus)

defprint1post()

: one_four_one(nothing, print_space, print_bar)

defprint4beams()

: one_four_one(print_plus, print1beam, print_end)

defprint4posts()

: one_four_one(print_bar, print1post, print_end)

defprint_row()

: one_four_one(nothing, print4posts, print4beams)

defprint_grid()

: one_four_one(print4beams, print_row, nothing)

print_grid(

)comment =

"""after writing a draft of the 4x4 grid, i noticed that many of the

functions had the same structure: they would do something, do

something else four times, and then do something else once.

so i wrote one_four_one, which takes three functions as arguments; it

calls the first one once, then uses do_four to call the second one

four times, then calls the third.

then i rewrote print1beam, print1post, print4beams, print4posts,

print_row and print_grid using one_four_one.

programming is an exploratory process. writing a draft of a program

often gives you insight into the problem, which might lead you to

rewrite the code to reflect the structure of the solution.

--- allen

"""print

(comment)

本人修改之後的**:

from __future__ import print_function, division

defprint_beam()

:print

('+ - - - -'

, end=

' ')

defprint_add()

:print

("+"

)def

print_post()

:print

('| '

, end=

' ')

defprint_line()

:print

("|"

)def

do_sometime

(f, n)

:for i in

range(0

, n)

: f(

)def

print_grid

(n):

for i in

range(0

, n)

: do_sometime(print_beam, n)

print_add(

)for j in

range(0

,4):

do_sometime(print_post, n)

print_line()

do_sometime(print_beam, n)

print_add(

)print_grid(2)

print_grid(

4)

運**況:

+--

--+-

---+

||||

||||

||||

+---

-+--

--+|

||||

||||

|||+

----

+---

-++-

---+

----

+---

-+--

--+|

||||

||||

||||

||||

|||+

----

+---

-+--

--+-

---+

||||

||||

||||

||||

||||

+---

-+--

--+-

---+

----

+|||

||||

||||

||||

||||

|+--

--+-

---+

----

+---

-+||

||||

||||

||||

||||

||+-

---+

----

+---

-+--

--+

Ruby on Rails 乙個完整的例子(四)

這個例子的一開始,我們是通過訪問rails的welcome頁面進行展示的。現在讓我們通過連線從welcome頁面遷移到article頁面。我們對welcome index.html.erb檔案變更如下 h1 hello,rails link to my blog controller article...

peewee 乙個輕量級的ORM 四

class database last insert id cursor,model parameters return type 最後乙個插入的記錄的那行的主鍵,不一定非得叫 id rows affected cursor return type 受影響的行數 create table model...

使用python的乙個小坑

今天進行乙個python 的小練習,因為是從頭開始練,第一次當然是運算子了,所以使用pycharm建立了乙個operator.py的檔案,前期沒什麼問題,但是到函式 這一點,需要匯入乙個類才行。發現出現了如下錯誤。各種對比發現沒有什麼不同。最終無奈的把錯誤貼到搜尋,雖然問題不同但是型別基本一樣。才發...