按照我的方式工作學習Python困難之路ex.25,而我無法將我的頭圍繞在某個東西上。這裏的腳本:爲什麼需要定義一些參數,而其他的不是? (Learn Python the Hard Way,ex。25)
def break_words(stuff):
"""this function will break waords up 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 ones."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)
運行腳本時,break_words會用我創造的任何說法,如果我用命令break_words(* *)。所以,我可以輸入
sentence = "My balogna has a first name, it's O-S-C-A-R"
,然後運行break_words(句),並與解析「'我 'balogna' '有'(...)結束。
但是,其他功能(如sort_words )將只接受與名稱的函數「的話,」我必須鍵入 字= break_words(句子)
或東西sort_words工作。
我爲什麼可以傳遞任何參數在括號break_words,但只有實際歸因於的論點「sentence」和「words」專門用於sort_words,print_first_and_last等?我覺得在我繼續閱讀本書之前,這是我應該理解的基本內容,而且我無法理解它。
這並不完全清楚你有什麼問題。請編輯您的問題,以包含一些示例程序以及您期望的輸出和實際獲得的輸出。 – SingleNegationElimination