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

边栏推荐
- 10 minute fitness method reading notes (2/5)
- Taobao order amount check error, avoid capital loss API
- Docker configures redis and redis clusters
- Lepton 无损压缩原理及性能分析
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
- RHCSA7
- Pytoch counts the number of the same elements in the tensor
- 【Nacos云原生】阅读源码第一步,本地启动Nacos
- RHCSA1
- GPON other manufacturers' configuration process analysis
猜你喜欢

SAP UI5 ObjectPageLayout 控件使用方法分享

VoneDAO破解组织发展效能难题

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

Taobao order amount check error, avoid capital loss API

Install rhel8.2 virtual machine

CVPR 2022 | single step 3D target recognizer based on sparse transformer

ActiveMQ installation and deployment simple configuration (personal test)

Distributed cache architecture - cache avalanche & penetration & hit rate

Iterator details in list... Interview pits

激动人心!2022开放原子全球开源峰会报名火热开启!
随机推荐
RHCSA2
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
SAP UI5 DynamicPage 控件介绍
SAP SEGW 事物码里的 Association 建模方式
CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
How do e-commerce sellers refund in batches?
UNIX socket advanced learning diary - advanced i/o functions
[cloud native] use of Nacos taskmanager task management
Full text search of MySQL
Kotlin process control and circulation
Compilation principle reading notes (1/12)
Sqoop import and export operation
由扫地增而引起的小叙
国内市场上的BI软件,到底有啥区别
滴滴开源DELTA:AI开发者可轻松训练自然语言模型
RHCSA5
Introduction to the principle of DNS
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Research: data security tools cannot resist blackmail software in 60% of cases