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

边栏推荐
- Talk about my drawing skills in my writing career
- 10 minute fitness method reading notes (3/5)
- C alarm design
- 在家庭智能照明中应用的测距传感芯片4530A
- Making and using the cutting tool of TTF font library
- 10 minute fitness method reading notes (1/5)
- Common commands and basic operations of Apache Phoenix
- How do e-commerce sellers refund in batches?
- Notes for preparation of information system project manager --- information knowledge
- 2021-12-21 transaction record
猜你喜欢

Pytoch implements tf Functions of the gather() function

Transactions from January 14 to 19, 2022

Preliminary exploration of basic knowledge of MySQL

I met Tencent in the morning and took out 38K, which showed me the basic smallpox

RHCSA3

Tips and tricks of image segmentation summarized from 39 Kabul competitions

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

你的下一台电脑何必是电脑,探索不一样的远程操作

实战模拟│JWT 登录认证

石臻臻的2021总结和2022展望 | 文末彩蛋
随机推荐
Laravel文档阅读笔记-mews/captcha的使用(验证码功能)
stirring! 2022 open atom global open source summit registration is hot!
JDBC -- use JDBC connection to operate MySQL database
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Taobao short video, why the worse the effect
[cloud native] use of Nacos taskmanager task management
mysql拆分字符串做条件查询
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
谈谈我写作生涯的画图技巧
Taobao flag insertion remarks | logistics delivery interface
Alipay transfer system background or API interface to avoid pitfalls
Principle of universal gbase high availability synchronization tool in Nanjing University
Taobao, pinduoduo, jd.com, Doudian order & Flag insertion remarks API solution
SAP 自开发记录用户登录日志等信息
Add a new cloud disk to Huawei virtual machine
#yyds干货盘点#js截取文件后缀名
Making and using the cutting tool of TTF font library
10 minute fitness method reading notes (3/5)
Kotlin变量
Compile kernel modules separately