《笨辦法學 python3》系列練習計劃——24.更多練習

題目

據說我們來到 24 題就已經距離第一部分的結束不遠了。我們學習瞭如何打印 print 還能夠同時運用格式化字符 % 和轉義字符 \ ,我們也已經能夠寫一些函數 def ,並且知道如何把寫好的腳本引入當前腳本 import ……Zed 認爲我們還需要練習來鞏固學過的知識,我想不會有人覺得更多的練習不好,那麼本題將是一個比較長的練習,據說下一題也是。

加分練習

  1. 檢測、倒着檢測、朗讀代碼、用註釋理清思路。這些我們很熟悉了。
  2. 故意寫錯一些代碼,看看會有什麼樣的錯誤,確定自己能夠修正它們。




我的答案

24.0 基礎練習 + 24.1 註釋

# 開場問候一下
print("Let's practice everything.")

# 轉義字符練習,這裏還有一個英文口語知識點啊 -_-||| 'bout == about
print('You\'d need to know \'bout escapes with \\ that do \nnewlines 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("-" * 15)

# 變量
five = 10 - 2 + 3 - 6
# 格式化字符
print("This should be five: %s" % five)

# 函數來了,還悄悄高速我們如何 return 多個值
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))

這裏寫圖片描述




返回目錄

《笨辦法學 python3》系列練習計劃——目錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章