当前位置:网站首页>函数传递参数小案例
函数传递参数小案例
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)
边栏推荐
- CVPR 2022 | single step 3D target recognizer based on sparse transformer
- How can labels/legends be added for all chart types in chart. js (chartjs.org)?
- 由扫地增而引起的小叙
- Didi open source Delta: AI developers can easily train natural language models
- Kotlin变量
- 10 minute fitness method reading notes (5/5)
- Time conversion error
- Tips and tricks of image segmentation summarized from 39 Kabul competitions
- 2021.12.16-2021.12.20 empty four hand transaction records
- A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this
猜你喜欢
石臻臻的2021总结和2022展望 | 文末彩蛋
激动人心!2022开放原子全球开源峰会报名火热开启!
[cloud native] event publishing and subscription in Nacos -- observer mode
Four common problems of e-commerce sellers' refund and cash return, with solutions
JDBC -- extract JDBC tool classes
Pytoch implements tf Functions of the gather() function
HiEngine:可媲美本地的云原生内存数据库引擎
Distance measuring sensor chip 4530a used in home intelligent lighting
[Nacos cloud native] the first step of reading the source code is to start Nacos locally
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
随机推荐
Wechat enterprise payment to change access, open quickly
What is the difference between Bi software in the domestic market
实战模拟│JWT 登录认证
Add a new cloud disk to Huawei virtual machine
Transactions from January 6 to October 2022
Kotlin变量
Kotlin process control and circulation
Introduction to relational model theory
SAP UI5 FlexibleColumnLayout 控件介绍
RHCSA4
自然语言处理从小白到精通(四):用机器学习做中文邮件内容分类
《信息系统项目管理师》备考笔记---信息化知识
Tips and tricks of image segmentation summarized from 39 Kabul competitions
2021.12.16-2021.12.20 empty four hand transaction records
HiEngine:可媲美本地的云原生内存数据库引擎
ActiveMQ installation and deployment simple configuration (personal test)
Sqoop import and export operation
View and terminate the executing thread in MySQL
HiEngine:可媲美本地的云原生内存数据库引擎
激动人心!2022开放原子全球开源峰会报名火热开启!