2012-08-08 19 views
0

我剛剛開始學習Python幾個小時前,似乎有一個問題,我根本無法得到。Stuck at Learnpython.org教程(關於函數)

他們問我:

  1. 添加一個功能叫list_benefits() - 返回字符串以下列表:「更多有組織的代碼」,「更可讀的代碼」,「易代碼重用」, 「允許程序員共享並連接代碼」

  2. 添加一個名爲build_sentence(info)的函數,它接收包含字符串的單個參數,並返回以給定字符串開頭並以字符串結尾的句子「是功能!」

  3. 運行並看到所有的功能一起工作!

我GOOGLE了這個問題,但他們都似乎是Python之前的版本中,我希望獲得更新的方式做到這一點。

定的代碼:

def name_the_benefits_of_functions(): 
    list_of_benefits = list_benefits() 
    for benefit in list_of_benefits: 
     print build_sentence(benefit) 

name_the_benefits_of_functions() 

預期輸出:

More organized code is a benefit of functions! 
More readable code is a benefit of functions! 
Easier code reuse is a benefit of functions! 
Allowing programmers to share and connect code together is a benefit of functions! 

我曾嘗試:

def list_benefits(): 
    benefits_list = ["More organized code", "More readable code", "Easier code reuse",   "Allowing programmers to share and connect code together"] 
    return benefits_list 
def build_sentence(benefit): 
    return "%s is a benefit of functions!" % list_benefits() 

def name_the_benefits_of_functions(): 
    list_of_benefits = list_benefits() 
    for benefit in list_of_benefits: 
     print(build_sentence(benefit)) 

name_the_benefits_of_functions() 

輸出:

['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions! 
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions! 
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions! 
['More organized code', 'More readable code', 'Easier code reuse', 'Allowing programmers to share and connect code together'] is a benefit of functions! 

任何人都可以告訴我我做錯了什麼?

回答

2

每次調用build_sentence()功能時,你只希望它使用一個好處,你在benefit參數指定搭建一個句子。

def build_sentence(benefit): 
    return "%s is a benefit of functions!" % benefit 

對於這個循環的每個迭代:

for benefit in list_of_benefits: 
    print(build_sentence(benefit)) 

一個好處傳遞給build_sentence()功能,那就是要打印的內容。

+0

非常感謝,他們都是很好的答案,我很震驚人們在這裏迴應的速度有多快! :) – Inveritatem 2012-08-08 03:37:21

1

我想你想要做的是:

def build_sentence(benefit): 
    return "%s is a benefit of functions!" % benefit 

你在name_the_benefits_of_functions調用list_benefits()被存儲在本地變量list_of_benefits結果的列表。現在你重複了這個(正確的),但是在你的build_sentence函數中,你會重複獲得一個新的好處列表。而不是那樣做,只需添加傳入的單個benefit

我知道你是Python新手,非常歡迎。我相信你會到generators的部分,但這裏是一個修改的例子,使用它的樂趣。

def list_benefits(): 
    benefits_list = ["More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"] 
    i = 0 
    while i < len(benefits_list): 
     yield benefits_list[i] 
     i += 1 
0

當您應該將函數中的每個字符串依次傳遞給函數build_sentence時,您正在傳遞字符串列表。

list_of_benefits = list_benefits() 
for item in list_of_benefits: 
    print build_sentence(item) 

您還需要在此處設置問題的格式,以便更容易解密。

我希望我能正確理解你的問題。

1

我剛剛陷入了同樣的問題。我name_the_benefits_of_functions()功能是有點廢話,因爲它只是重複四次,而不是優雅地失敗時list_of_benefits耗盡,但在這裏是爲我工作:

# Modify this function to return a list of strings as defined above 
def list_benefits(count): #just a list of benefits. It spits out whatever number 'count' happens to be 
    list_of_benefits = ["More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together"] 
return list_of_benefits[count] 

# Modify this function to concatenate to each benefit - " is a benefit of functions!" 
def build_sentence(benefit): #tacks on the sentence end, and that's it 
    return "%s is a benefit of functions!" % benefit 

def name_the_benefits_of_functions(): 
    count = 0 
    while count < 4: # not very graceful, but oh well 
     benefit = list_benefits(count) 
     print build_sentence(benefit) 
     count += 1 

name_the_benefits_of_functions() 
0

這是對我工作。我花了一段時間纔得到首字母縮略詞,錯誤和所有類型的煙花,但正確的答案。最後......我意識到你並不需要所有功能的意大利麪(就像他們設定的那樣) - 我試圖讓這些功能按我認爲是必需的工作;但它是沒有的。所以,我很驚訝,我得到了彈出移動到下一個章節。

課我學到了什麼?..要回報打印,並用它進行區分。

唔夠稱。這裏是代碼!:

# Modify this function to return a list of strings as defined above 
s= ("More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together") 
def list_benefits(s): 
    for b in s: 
    return b 

def build_sentences(s): 
    for b in s: 
    print b + " is a benefit of functions!" 

list_benefits(s) 
build_sentences(s) 
0

我是一個初學者,這是我的答案,工作

高清list_benefits():

benefit_list= ["More organized code","More readable code", "Easier code reuse","Allowing programmers to share and connect code together"] 

return benefit_list 
pass 

高清build_sentence(利益):

return benefit + " is a benefit of functions!" 

pass 

高清name_the_benefits_of_functions():

list_of_benefits = list_benefits() 

for benefit in list_of_benefits: 

    print build_sentence(benefit) 

name_the_benefits_of_fu nctions()

0

ANSWER(短&甜):

# Modify this function to return a list of strings as defined above 
def list_benefits(): 
    return "More organized code", "More readable code", "Easier code reuse", "Allowing programmers to share and connect code together" 



# Modify this function to concatenate to each benefit - " is a benefit of functions!" 
def build_sentence(benefit): 
    return "%s is a benefit of functions!" % benefit 


def name_the_benefits_of_functions(): 
    list_of_benefits = list_benefits() 
    for benefit in list_of_benefits: 
     print build_sentence(benefit) 

name_the_benefits_of_functions() 

擺脫你的列表,只返回字符串。