鍍金池/ 教程/ Python/ exercise24. 更多的練習(xí)
附錄 A-練習(xí) 9:生成一個(gè)空文件(Touch, New-Item)
附錄 A-練習(xí) 10:復(fù)制文件 (cp)
exercise44.繼承 Vs.包含
附錄 A-練習(xí) 14:刪除文件 (rm)
附錄 A-練習(xí) 11:移動(dòng)文件 (mv)
exercise46.項(xiàng)目骨架
附錄 A-練習(xí) 3:如果你迷路了
exercise37.復(fù)習(xí)符號
exercise47.自動(dòng)化測試
exercise3.數(shù)字和數(shù)學(xué)計(jì)算
附錄 A-練習(xí) 1:安裝
exercise32.循環(huán)和列表
exercise31.做出決定
exercise42.對象、類、以及從屬關(guān)系
exercise48.更復(fù)雜的用戶輸入
下一步
簡介
附錄 A-練習(xí) 7:刪除路徑 (rmdir)
exercise49.寫代碼語句
exercise18.命名, 變量, 代碼, 函數(shù)
exercise12.提示別人
exercise14.提示和傳遞
exercise40.模塊, 類和對象
附錄 A-練習(xí) 12:查看文件 (less, MORE)
exercise9.打印, 打印, 打印
exercise13.參數(shù), 解包, 變量
exercise30. Else 和 If
exercise28. 布爾表達(dá)式
附錄 A-練習(xí) 4:創(chuàng)建一個(gè)路徑 (mkdir)
附錄 A-練習(xí) 15:退出命令行 (exit)
exercise25. 更多更多的練習(xí)
exercise6.字符串和文本
exercise2.注釋和井號“#”
exercise21. 函數(shù)的返回值
附錄 A-下一步
exercise1.第一個(gè)程序
exercise23. 閱讀代碼
附錄 A-練習(xí) 5:改變當(dāng)前路徑 (cd)
exercise17.更多文件操作
exercise24. 更多的練習(xí)
exercise19.函數(shù)和變量
exercise51.從瀏覽器獲取輸入
exercise22. 到目前為止你學(xué)到了什么?
exercise41.學(xué)會(huì)說面向?qū)ο?/span>
exercise52.開始你的 web 游戲
exercise20. 函數(shù)和文件
exercise15.讀文件
exercise45.你來制作一個(gè)游戲
exercise10.那是什么?
exercise8.打印, 打印
exercise35.分支和函數(shù)
exercise26. 恭喜你,可以進(jìn)行一次考試了
exercise33.while 循環(huán)
exercise29. IF 語句
exercise36.設(shè)計(jì)和調(diào)試
exercise0.安裝和準(zhǔn)備
exercise50.你的第一個(gè)網(wǎng)站
附錄 A-練習(xí) 2:路徑, 文件夾, 名錄 (pwd)
exercise38.列表操作
附錄 A-練習(xí) 6:列出當(dāng)前路徑 (ls)
exercise16.讀寫文件
exercise4.變量和命名
exercise34.訪問列表元素
exercise11.提問
exercise43.基本的面向?qū)ο蟮姆治龊驮O(shè)計(jì)
附錄 A-簡介
附錄 A-練習(xí) 8:目錄切換(pushd, popd)
來自老程序員的建議
exercise27. 記住邏輯
exercise5.更多的變量和打印
exercise7.更多的打?。ㄝ敵觯?/span>
附錄 A-練習(xí) 13:輸出文件 (cat)
exercise39.字典,可愛的字典

exercise24. 更多的練習(xí)

你離這本書第一部分的結(jié)尾已經(jīng)不遠(yuǎn)了,你應(yīng)該已經(jīng)具備了足夠的 Python 基礎(chǔ)知識,可以繼續(xù)學(xué)習(xí)一些編程的原理了,但你應(yīng)該做更多的練習(xí)。這個(gè)練習(xí)的內(nèi)容比較長,它的目的是鍛煉你的毅力,下一個(gè)習(xí)題也差不多是這樣的,好好完成它們,做到完全正確,記得仔細(xì)檢查。

print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
"""

print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 6
print "This should be five: %s" % five

def secret_formula(started):
    jelly_beans = started * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates

start_point = 10000
beans, jars, crates = secret_formula(start_point)

print "With a starting point of: %d" % start_point
print "We'd have %d beans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crates." % secret_formula(start_point)

你看到的結(jié)果

$ python ex24.py
Let's practice everything.
You'd need to know 'bout escapes with \ that do
 newlines and   tabs.
--------------

       The lovely world
with logic so firmly planted
cannot discern
 the needs of love
nor comprehend passion from intuition
and requires an explanation

              where there is none.

--------------
This should be five: 5
With a starting point of: 10000
We'd have 5000000 beans, 5000 jars, and 50 crates.
We can also do that this way:
We'd have 500000 beans, 500 jars, and 5 crates.

附加題

1.記得仔細(xì)檢查結(jié)果,從后往前倒著檢查,把代碼朗讀出來,在不清楚的位置加上注釋。 2.故意把代碼改錯(cuò),運(yùn)行并檢查會(huì)發(fā)生什么樣的錯(cuò)誤,并且確認(rèn)你有能力改正這些錯(cuò)誤。

常見問題

Q:為什么在后面你把 jelly_beans 這個(gè)變量叫做 beans?

這是函數(shù)如何工作的一部分。記住,在函數(shù)中,變量都是臨時(shí)的.當(dāng)你返回一個(gè)變量的時(shí)候,你可以把它賦值給另一個(gè)變量。我只是定義了一個(gè)叫做‘beans’的新變量去接收返回值。

Q:你所說的反向閱讀代碼是什么意思?

從最后一行開始閱讀。對比你的代碼和我的是不是一樣。如果確認(rèn)一樣,向上移動(dòng)一行閱讀,直到你讀到腳本的第一行為止。

Q:那首詩是誰寫的?

我寫的。