当前位置:网站首页>第018讲:函数:灵活即强大 | 课后测试题及答案
第018讲:函数:灵活即强大 | 课后测试题及答案
2022-06-22 20:05:00 【ChaseTimLee】
测试题:
0. 请问以下哪个是形参哪个是实参?
def MyFun(x):
return x ** 3
y = 3
print(MyFun(y))
x 是形式参数(形参),y 是实际参数(实参)。
跟绝大部分编程语言一样,形参指的是函数创建和定义过程中小括号里的参数,而实参指的是函数在调用过程中传递进去的参数。
1. 函数文档和直接用“#”为函数写注释有什么不同?
给函数写文档是为了让别人可以更好的理解你的函数,所以这是一个好习惯
函数的文档字符串可以按如下方式访问: MyFirstFunction.__ doc __我们用 help() 来访问这个函数也可以看到这个文档字符串
2. 使用关键字参数,可以有效避免什么问题的出现呢?
使用关键字参数,可以有效避免因不小心搞乱参数的顺序导致的 BUG 出现。
3. 使用 help(print) 查看 print() 这个 BIF 有哪些默认参数?分别起到什么作用?
>>> help(print)
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.
# 文件类型对象,默认是sys.stdout(标准输出流)
sep: string inserted between values, default a space.
# 第一个参数如果有多个值(第一个参数是收集参数),各个值之间默认用空格(space)隔开
end: string appended after the last value, default a newline.
# 打印最后一个值之后默认参数一个新行标识符(‘\n’)
flush: whether to forcibly flush the stream.
# 是否强制刷新流
4. 默认参数和关键字参数表面最大的区别是什么?
呃,其实这道题考大家有没有认真听课啦~
关键字参数是在函数调用的时候,通过参数名制定需要赋值的参数,这样做就不怕因为搞不清参数的顺序而导致函数调用出错。而默认参数是在参数定义的过程中,为形参赋初值,当函数调用的时候,不传递实参,则默认使用形参的初始值代替。
动动手:
0. 编写一个符合以下要求的函数:
计算打印所有参数的和乘以基数(base=3)的结果(比如 mFun(1, 2, 3, 4, 5) 的结果为 45)
如果参数中最后一个参数为(base=5),则设定基数为 5,基数不参与计算(比如 mFun(1, 2, 3, 4, 5, base=5) 的结果为 75)。
def mFun(*param, base=3):
result = 0
for each in param:
result += each
result *= base
print('结果是:', result)
mFun(1, 2, 3, 4, 5, base=5)
1. 寻找水仙花数
题目要求:如果一个 3 位数等于其各位数字的立方和,则称这个数为水仙花数。
例如 153 = 13+53+3^3,因此 153 是一个水仙花数。编写一个程序,找出所有的水仙花数。
def Narcissus():
for each in range(100, 1000):
temp = each
sum = 0
while temp:
sum = sum + (temp%10) ** 3
temp = temp // 10 # 注意这里用地板除
if sum == each:
print(each, end='\t')
print("所有的水仙花数分别是:", end='')
Narcissus()
2. 编写一个函数 findstr(),该函数统计一个长度为 2 的子字符串在另一个字符串中出现的次数。
例如:假定输入的字符串为 “You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.”,子字符串为 “im”,函数执行后打印“子字母串在目标字符串中共出现 3 次”。
def findStr(desStr, subStr):
count = 0
length = len(desStr)
if subStr not in desStr:
print('在目标字符串中未找到字符串!')
else:
for each1 in range(length-1):
if desStr[each1] == subStr[0]:
if desStr[each1+1] == subStr[1]:
count += 1
print('子字符串在目标字符串中共出现 %d 次' % count)
desStr = input('请输入目标字符串:')
subStr = input('请输入子字符串(两个字符):')
findStr(desStr, subStr)
边栏推荐
- 第020讲:函数:内嵌函数和闭包 | 课后测试题及答案
- 71- analysis of an Oracle DBA interview with Alibaba in 2010
- 使用Charles抓包
- Ultrafast transformers | redesign vit with res2net idea and dynamic kernel size, surpassing mobilevit
- 【138. 复制带随机指针的链表】
- 杰理之开启四声道通话近端变调问题【篇】
- One line of code binds swiftui view animation for a specific state
- HarmonyOS应用开发培训第二次
- ACM. HJ45 名字的漂亮度 ●●
- redis学习笔记
猜你喜欢
随机推荐
Ultrafast transformers | redesign vit with res2net idea and dynamic kernel size, surpassing mobilevit
Baijia forum Wu Zetian
71- analysis of an Oracle DBA interview with Alibaba in 2010
[redis]Redis6的事务操作
Apple corefoundation source code
2022团体程序设计天梯赛L1
Apple Objective-C source code
53 page intelligent campus intelligent system design scheme (download attached)
为了不曾忘却的纪念:孙剑专题
73- find the SQL example during the business peak period (report development class)
Watch,computed和methods的区别
74- how to remedy the loss of Oracle to MySQL for this kind of SQL optimization?
2022年山东省安全员C证考试试题模拟考试平台操作
[redis]发布与订阅
查询es分页下标超过1万
嵌入式开发基础之任务管理(线程管理)
杰理之开启四声道通话近端变调问题【篇】
2022 chemical automation control instrument examination exercises and online simulation examination
[redis]redis persistence
[the penultimate node in the linked list]









