当前位置:网站首页>函数传递参数小案例
函数传递参数小案例
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)
边栏推荐
- Four common problems of e-commerce sellers' refund and cash return, with solutions
- Introduction to the principle of DNS
- Install rhel8.2 virtual machine
- Taobao product details API | get baby SKU, main map, evaluation and other API interfaces
- Tips and tricks of image segmentation summarized from 39 Kabul competitions
- Using MySQL in docker
- Kotlin流程控制、循环
- HiEngine:可媲美本地的云原生内存数据库引擎
- 太方便了,钉钉上就可完成代码发布审批啦!
- Transactions from December 29, 2021 to January 4, 2022
猜你喜欢
Research: data security tools cannot resist blackmail software in 60% of cases
《信息系统项目管理师》备考笔记---信息化知识
Preliminary exploration of basic knowledge of MySQL
I'm doing open source in Didi
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
SAP 自开发记录用户登录日志等信息
Why is your next computer a computer? Explore different remote operations
开发者,云原生数据库是未来吗?
Volatile instruction rearrangement and why instruction rearrangement is prohibited
SAP UI5 DynamicPage 控件介绍
随机推荐
ActiveMQ installation and deployment simple configuration (personal test)
SAP 自开发记录用户登录日志等信息
insmod 提示 Invalid module format
非技术部门,如何参与 DevOps?
Redis master-slave configuration and sentinel mode
Keras implements verification code identification
Simply take stock reading notes (2/8)
Didi open source Delta: AI developers can easily train natural language models
Why is your next computer a computer? Explore different remote operations
奔跑,开路
Distributed solution - Comprehensive decryption of distributed task scheduling platform - xxljob scheduling center cluster
How can non-technical departments participate in Devops?
RHCAS6
10 minute fitness method reading notes (2/5)
HiEngine:可媲美本地的云原生内存数据库引擎
Pinduoduo flag insertion remarks API
Transactions from January 14 to 19, 2022
Pytoch through datasets Imagefolder loads datasets directly from files
Redis cluster configuration
初识Linkerd项目