第 17 课
把做法装成工具
1 / 1 · ← → 翻页
第 17 课
食谱写一次,就能用许多次。

学习目标
自己写函数,也会用内置函数。
打招呼、算面积、看分数统计。
情境

炒饭做法写好,三盘都按它做。
不用从头再抄三遍。
想一想

例题

def say_hello():
print("你好")
say_hello()
say_hello()做一做

def say_hello():say_hello()请学生指出哪一行是调用。
例题

def greet(name):
print("你好,", name)
greet("小派")
greet("小红")例题

def rectangle_area(w, h):
return w * h
a = rectangle_area(3, 4)
print(a)想一想
只 print,外面拿不到数。
要带走结果,用 return。
情境

有些工具,Python 已经准备好了。
像教室里的公用尺子。
例题

scores = [90, 85, 92, 70]
print(min(scores))
print(max(scores))
print(sum(scores))
print(len(scores))例题

做一做

avg = sum(scores) / len(scores)
print(round(avg, 1))请运行,核对平均分。
做一做

请学生用四个内置函数说出结果。
小结

练一练

练一练
rectangle_area(3, 4)
返回多少?
下一课
找东西和修 bug?
