[Python]小甲魚Python視頻第018課(函數:靈活即強大)課後題及參考解答
阿新 • • 發佈:2019-03-09
sub dst 出錯 += 參數表 efault 註釋 module 試題
# -*- coding: utf-8 -*- """ Created on Wed Mar 6 19:28:00 2019 @author: Administrator """ """ 測試題: 0. 請問以下哪個是形參哪個是實參? def MyFun(x): return x ** 3 y = 3 print(MyFun(y)) y是實參 x是形參 1. 函數文檔和直接用“#”為函數寫註釋有什麽不同? 函數文檔:用隱藏屬性指向字符串,函數文檔的內容會進內存 #:文件註釋,運行時內存裏面是沒有的 2. 使用關鍵字參數,可以有效避免什麽問題的出現呢? 確保傳參時一一對應,指定對應防止傳參出錯 3. 使用help(print)查看print()這個BIF有哪些默認參數?分別起到什麽作用? Help on built-in function print in module builtins: print(...) print(value, ..., sep=‘ ‘, end=‘\n‘, file=sys.stdout, flush=False) Prints the values to a stream, or to sys.stdout by default. Optional keyword arguments: file: a file-like object (stream); defaults to the current sys.stdout. sep: string inserted between values, default a space. end: string appended after the last value, default a newline. flush: whether to forcibly flush the stream. sep 分隔符 end 末尾 file 輸出對象 flush 是否刷新緩存 4. 默認參數和關鍵字參數表面最大的區別是什麽? ......風馬流不相及。。。。。 動動手 0. 編寫一個符合以下要求的函數: a) 計算打印所有參數的和乘以基數(base=3)的結果 b) 如果參數中最後一個參數為(base=5),則設定基數為5,基數不參與求和計算。 1. 尋找水仙花數 題目要求:如果一個3位數等於其各位數字的立方和,則稱這個數為水仙花數。例如153 = 1^3+5^3+3^3,因此153是一個水仙花數。編寫一個程序,找出所有的水仙花數。 2. 編寫一個函數 findstr(),該函數統計一個長度為 2 的子字符串在另一個字符串中出現的次數。例如:假定輸入的字符串為“You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.”,子字符串為“im”,函數執行後打印“子字母串在目標字符串中共出現 3 次”。 3. 請寫下這一節課你學習到的內容:格式不限,回憶並復述是加強記憶的好方式! 額 """ #動動手0 def BaseSum(*para,base = 3): result = 0 ; for each in para: result += each; result *= base; return result; #動動手1: def dds1_sxh(): result = []; for each in range(100,1000): ge = each % 10; shi = (each // 10) % 10; bai = (each // 100) % 10; if each == ge**3 + shi**3 + bai**3: result.append(each); print(result); dds1_sxh(); #動動手2: def dds2_findstr(str_full,str_sub): length_full = len(str_full); length_sub = len(str_sub); result_count = 0; if length_sub>length_full or length_sub !=2: return None; for idx in range(length_full-1): if str_full[idx:idx+2] == str_sub: result_count += 1; return result_count; str_full = "You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted"; str_sub = ‘im‘; print(dds2_findstr(str_full,str_sub))
[Python]小甲魚Python視頻第018課(函數:靈活即強大)課後題及參考解答