当前位置:网站首页>第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)
边栏推荐
- [redis] cluster and common errors
- Use Charles to capture packets
- [cm11 linked list splitting]
- 70 root cause analysis Oracle database sudden performance problems, who will take the blame
- HarmonyOS应用开发培训第二次
- 日本动漫作家和其部分作品
- [redis]redis6 transaction operations
- redis学习笔记
- NBA playoff match chart
- 杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】
猜你喜欢
随机推荐
Use Charles to capture packets
【142. 环形链表 II】
[513. find the value in the lower left corner of the tree]
71- analysis of an Oracle DBA interview with Alibaba in 2010
【160. 相交链表】
Watch,computed和methods的区别
Flutter System Architecture(Flutter系统架构图)
Final review of scientific and technological literature of Shandong University (Personal Crash Course)
90- review of several recently optimized Oracle databases
92 match for several_ Recognize SQL write example
数据库总结:mysql在开发过程中常见的问题及优化
[206. reverse linked list]
第027讲:集合:在我的世界里,你就是唯一 | 课后测试题及答案
Japanese anime writers and some of their works
PlainSelect. getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
74- how to remedy the loss of Oracle to MySQL for this kind of SQL optimization?
94-SQL优化案例一则(用到的写法经常是被嫌弃的)
[redis]集群与常见错误
【OR36 链表的回文结构】
《跟唐老师学习云网络》 - OpenStack网络实现





![[redis]redis的持久化操作](/img/83/9af9272bd485028062067ee2d7a158.png)
![[palindrome structure of or36 linked list]](/img/67/33730a30c715db573e1d4f7e5556da.png)


