当前位置:网站首页>函数传递参数小案例
函数传递参数小案例
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)

边栏推荐
- Halcon 模板匹配实战代码(一)
- SAP SEGW 事物码里的 Association 建模方式
- 自然语言处理从小白到精通(四):用机器学习做中文邮件内容分类
- Distance measuring sensor chip 4530a used in home intelligent lighting
- Introduction to the principle of DNS
- 奔跑,开路
- Taobao order amount check error, avoid capital loss API
- #yyds干货盘点#js截取文件后缀名
- Simply take stock reading notes (1/8)
- RHCSA7
猜你喜欢

Taobao short videos are automatically released in batches without manual RPA open source

Research: data security tools cannot resist blackmail software in 60% of cases

Transactions from December 27 to 28, 2021

国内市场上的BI软件,到底有啥区别

What if wechat is mistakenly sealed? Explain the underlying logic of wechat seal in detail

Taobao short video, why the worse the effect

Pytorch two-layer loop to realize the segmentation of large pictures

A deep long article on the simplification and acceleration of join operation

SAP SEGW 事物码里的 ABAP Editor
![[cloud native] event publishing and subscription in Nacos -- observer mode](/img/0f/34ab42b7fb0085f58f36eb67b6f107.png)
[cloud native] event publishing and subscription in Nacos -- observer mode
随机推荐
GPON other manufacturers' configuration process analysis
Simply take stock reading notes (4/8)
Taobao short video, why the worse the effect
深度长文探讨Join运算的简化和提速
Distributed solution - completely solve website cross domain requests
Pytoch counts the number of the same elements in the tensor
Neural network of PRML reading notes (1)
mysql拆分字符串做条件查询
SAP UI5 ObjectPageLayout 控件使用方法分享
Why is your next computer a computer? Explore different remote operations
Yyds dry inventory JS intercept file suffix
I met Tencent in the morning and took out 38K, which showed me the basic smallpox
非技术部门,如何参与 DevOps?
Taobao order amount check error, avoid capital loss API
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
10 minute fitness method reading notes (3/5)
JDBC -- use JDBC connection to operate MySQL database
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
Full text search of MySQL
jxl笔记