看板 Python 關於我們 聯絡資訊
各位先進們好, 小廢物我目前在Coursera上面自學Python, 有個「應該」對各位蠻簡單的練習題(非考試題目)想發問。 In this code, identify the repeated pattern and replace it with a function called month_days, that receives the name of the month and the number of days in that month as parameters. Adapt the rest of the code so that the result is the same. Confirm your results by making a function call with the correct parameters for both months listed. # REPLACE THIS STARTER CODE WITH YOUR FUNCTION june_days = 30 print("June has " + str(june_days) + " days.") july_days = 31 print("July has " + str(july_days) + " days.") 我的問題: 1.上堂課是教要利用def month_days(這邊我不知道怎麼打) 這個題目不像是在計算, 都是名詞定義... 2.在Python中存在一種函數可以自動call一個月有幾天嗎Q_Q? 一個人搞2小時想破頭也弄不出來 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.212.239 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1623519787.A.D29.html
ck574b027: 先確認你有理解題目吧,翻譯那段英文看看 06/13 01:52
eric10252013: 題目是要能夠reuse the code 06/13 03:20
alvinlin: 其實題目寫得很清楚了。我幫你翻譯一下好了。首先你要定 06/13 03:36
alvinlin: 義一個函式,名字就叫 months_days。然後現在已經有一段 06/13 03:36
alvinlin: 程式,長得像你上面的那個樣子。然後因為函式是用來解決 06/13 03:36
alvinlin: 重複性的問題。所以你要從現在已經有的那段程式-即六月 06/13 03:36
alvinlin: 和七月-的這個範例裡面找出來他們彼此之間有什麼重複的 06/13 03:36
alvinlin: 型態。然後根據題目的要求,給予函式兩個參數。分別是月 06/13 03:36
alvinlin: 份以及該月份的日期總數。最後這個函式的輸出結果必須跟 06/13 03:36
alvinlin: 原本的程式結果相同。他的意思就是要輸出一樣的結果 06/13 03:36
alvinlin: 最後你必須在你的程式就是主程式的部分,呼叫這個函式然 06/13 03:39
alvinlin: 後給予相同的月份就是六月和七月,最後確認這個結果跟上 06/13 03:39
alvinlin: 面這段程式輸出的結果一模一樣 06/13 03:39
alvinlin: 再稍微白話一點就是你先把原本的程式執行一次然後看看輸 06/13 03:42
alvinlin: 出的結果是什麼。然後把這份程式改成用函式的方法來執行 06/13 03:42
alvinlin: 但是結果的輸出必須是相同的 06/13 03:42
alvinlin: 我感覺這題不是在考程式撰寫他是在考你的英文理解能力。 06/13 03:45
alvinlin: 你看我中文寫了這麼多他的英文才寫兩三句 06/13 03:45
eric10252013: 回樓上 我完全理解英文 但我不知道怎麼寫 06/13 12:12
eric10252013: 我有作答過寫出一樣的結果 我晚點把我寫到的答案po 06/13 12:13
eric10252013: 出來 06/13 12:13
panex0845: 2. https://glot.io/snippets/fzi79egpzz 06/13 14:13
alvinlin: https://i.imgur.com/2OICyX7.jpg 06/13 14:23
alvinlin: 好吧。幫你寫好了。 06/13 14:24
tzouandy2818: 你完全理解題意的話怎麼會解不出來 這根本不用解啊 06/13 18:57
eric10252013: 我卡住的點就是month_days(內文),因為前面的教學影 06/15 20:53
eric10252013: 片都只有掛號內數字,現在才知道也可以填文字,以及 06/15 20:53
eric10252013: 原來print(這邊還可以填function) 06/15 20:53
eric10252013: 感謝樓上大神們回覆! 06/15 20:53
JamesChen: 不知道可不可以填 第一個該做的就是試試看 06/22 07:56