当前位置:网站首页>函数传递参数小案例
函数传递参数小案例
2022-07-05 12:40:00 【南湖渔歌】
''' 编写如下函数: 函数名字,叫做computer 意思是计算 参数分别是x,y,method,代表数字x,数字y,字符串method 如果method==add字符串,返回x+y 如果method==sub字符串,返回x-y 如果method==mul字符串,返回x*y 如果method==div字符串,返回x/y method设置成带默认值的参数,默认为默认加法 分别用如下方式调用调用参数 位置参数 关键词参数和不带默认值参数调用 '''
def compute(x,y,method = 'add'):
if method == 'add':
print(f'{
x}+{
y}',x+y)
elif method == 'sub':
print(f'{
x}-{
y}',x-y)
elif method == 'mul':
print(f'{
x}*{
y}', x * y)
elif method == 'div':
print(f'{
x}/{
y}', x / y)
compute(3,4,'add')
compute(3,4,'sub')
compute(3,4,'mul')
# 如果想添加关键词参数,必须放在最后
compute(3,y=4,method='div')
# compute(3,y=4,'sub') # error:位置参数位于关键字实参后面会报错
compute(3,4)
compute(x=3,y=4)
边栏推荐
- Insmod prompt invalid module format
- RHCSA5
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
- Simply take stock reading notes (3/8)
- jxl笔记
- Transactions from December 29, 2021 to January 4, 2022
- Install rhel8.2 virtual machine
- Distributed solution - completely solve website cross domain requests
- 初识Linkerd项目
- Transactions from December 27 to 28, 2021
猜你喜欢
CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
Install rhel8.2 virtual machine
Pytoch implements tf Functions of the gather() function
Four common problems of e-commerce sellers' refund and cash return, with solutions
Transactions from December 29, 2021 to January 4, 2022
Research: data security tools cannot resist blackmail software in 60% of cases
Database connection pool & jdbctemplate
SAP 自开发记录用户登录日志等信息
Simply take stock reading notes (4/8)
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
随机推荐
【云原生】Nacos-TaskManager 任务管理的使用
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
开发者,云原生数据库是未来吗?
深度长文探讨Join运算的简化和提速
SAP 自开发记录用户登录日志等信息
Flume common commands and basic operations
【Nacos云原生】阅读源码第一步,本地启动Nacos
Free testing of Taobao tmall API order and flag insertion remark interface
Compile kernel modules separately
mysql拆分字符串做条件查询
Storage Basics
CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
SAP SEGW 事物码里的 Association 建模方式
Transactions from January 6 to October 2022
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
2021-12-22 transaction record
C alarm design
太方便了,钉钉上就可完成代码发布审批啦!
石臻臻的2021总结和2022展望 | 文末彩蛋