Exercise 26:恭喜你,現在可以參加考試了

原文鏈接:http://learnpythonthehardway.org/book/ex26.html

       你現在已經學習了這本書幾乎一半內容了。剩下的這一半內容將更加有趣。你將學到邏輯,並通過條件判斷來實現有用的功能。

       在你繼續學習之前,我們先要對你做一次考驗。這個考驗將會非常的難因爲它要求你修復別人的代碼。 當你成爲一個程序員的時候你常常需要去處理別的程序員的代碼並且時常也要忍受它們的傲慢態度。程序員時常宣稱他們的代碼時完美的。

       這種自以爲是不關心別人的程序員是愚蠢的。一個優秀的程序員會像一個優秀的科學家那樣承認他們的代碼總會有一定的機率會出錯。優秀的程序員在確認可能是由別人的代碼引起的錯誤之前,會先從發生錯誤的地方開始逐步排查自己的代碼中可能引起錯誤的地方。

       在這次的練習中,你將面對一個水平糟糕的程序員,並改好他的代碼。我將Exercise 24 和 25胡亂的拷貝到一個文件裏,然後隨機的移除了一些字符和 添加了一些錯誤進去。大多數的錯誤Python是會告訴你的,但是一些數學計算的錯誤需要你自己去找。以及在字符串中格式化錯誤或者拼寫錯誤也需要注意。

       所有的錯誤都是所有程序甚至有經驗的程序員經常會犯的錯誤。

       你這次練習的任務就是將這個文件修改正確。使用你會的所有技巧讓這個文件更加完善。先分析腳本,或者你可以像打印學期論文那樣將其打印出來。修復一個錯誤就運行一次,然後接着修復再運行如此操作直達腳本完美運行。不要試圖去尋求幫助,如果遇到你無法解決的你可以先將它放一放回過頭再來解決它。

       即使這個可能要花費你幾天時間去做,但是你也得努力通過這次考驗把這個腳本文件修改正確。

       最後,這次提到的這個練習不是自己輸入而是修復一個已經有的腳本文件。那樣做的話,你必須到廈門這個地址去下載相關文件:http://learnpythonthehardway.org/book/exercise26.txt

       把這個文本中的內容複製黏貼到命名爲ex26.py的文件中。僅僅這一次你允許去複製黏貼操作。

學生遇見的常見問題:


我應該將ex25.py文件作爲模塊引入還是就簡單移除ex25的引用操作?
答:兩種方法都可以。這個文件中的函數既然就是來自ex25 ,那麼你就先把它的引用去掉。

當我們修復了該文件是否能夠運行這個腳本文件?
答:肯定可以啊。電腦就在手邊,所以儘可能的使用電腦來幫助自己解決。

ps:附上自己修復好的文件:
def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print word

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print word

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)


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 explantion
\n\t\twhere there is none.
"""


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

five = 10 - 2 + 3 - 5
print "This should be five: %d" % 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 jeans, %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 crabapples." % secret_formula(start_point)


sentence = "All god\tthings come to those who weight."

words = break_words(sentence)
sorted_words = sort_words(words)

print words
print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = sort_sentence(sentence)
print sorted_words

print_first_and_last(sentence)

print_first_and_last_sorted(sentence)


發佈了23 篇原創文章 · 獲贊 7 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章