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

边栏推荐
- jxl笔记
- 10 minute fitness method reading notes (5/5)
- 《信息系统项目管理师》备考笔记---信息化知识
- Transactions from January 6 to October 2022
- Difference between JUnit theories and parameterized tests
- 我在滴滴做开源
- RHCSA3
- JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
- Using MySQL in docker
- How can non-technical departments participate in Devops?
猜你喜欢

Taobao short video, why the worse the effect

从39个kaggle竞赛中总结出来的图像分割的Tips和Tricks

初识Linkerd项目

stm32和电机开发(从架构图到文档编写)

【云原生】Nacos中的事件发布与订阅--观察者模式

Super efficient! The secret of swagger Yapi
![[Nacos cloud native] the first step of reading the source code is to start Nacos locally](/img/f8/d9b848593cf7380a6c99ee0a8158f8.png)
[Nacos cloud native] the first step of reading the source code is to start Nacos locally

RHCSA5

The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation

Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
随机推荐
How do e-commerce sellers refund in batches?
RHCSA7
Programming skills for optimizing program performance
SAP UI5 视图里的 OverflowToolbar 控件
Taobao order interface | order flag remarks, may be the most stable and easy-to-use interface
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
insmod 提示 Invalid module format
Transactions from January 6 to October 2022
NFT: how to make money with unique assets?
太方便了,钉钉上就可完成代码发布审批啦!
Kotlin function
【云原生】Nacos-TaskManager 任务管理的使用
Storage Basics
Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
Distance measuring sensor chip 4530a used in home intelligent lighting
Install rhel8.2 virtual machine
由扫地增而引起的小叙
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
SAP SEGW 事物码里的 Association 建模方式
Preliminary exploration of basic knowledge of MySQL