Ainsteinx Logo
Ainsteinx
👤
欢迎使用 Ainsteinx
登录

切换语言

货币

首页
博客
服务
全部课程
EP.7 老挝人的 Python

函数:写一次,用很多次

def total(price, tax=0.10):
    return price + price * tax

print(total(50000))
print(total(20000, 0))
视频即将上线
EP.7 函数与面向对象

函数:写一次,用很多次

别再复制粘贴代码了,来写个函数吧。

def 用来创建函数。传入的值叫作参数(parameter),return 把结果返回。

这个例子计算含 10% 税的总价。

ep07-functions.py
def total(price, tax=0.10):
    return price + price * tax

print(total(50000))
print(total(20000, 0))

输出结果

55000.0
20000
  • #PythonສຳລັບຄົນLaos
  • #Python
  • #ຮຽນໂປຣແກຣມ
  • #ບອກຕໍ່ຄວາມຮູ້
  • #shorts

观看并分享到