Python 官方文档:入门教程 => 点击学习
Switch语句用于编写多分枝结构的程序,类似与if…elif…else语句。Switch语句表达的分支结构比if…elif…else语句表达的更清晰,代码的可读性更高。但是python并没有提供switch语句Python通过定义字典,调
Switch语句用于编写多分枝结构的程序,类似与if…elif…else语句。
Switch语句表达的分支结构比if…elif…else语句表达的更清晰,代码的可读性更高。
但是python并没有提供switch语句
Python通过定义字典,调用字典的get()方法可以实现switch功能
#!/usr/bin/env python
from __future__ import division
def jsq(x,y,operator="error"):
result={"+":x+y,"-":x-y,"/":x*y,"/":x/y}
print result.get(operator)
if __name__=="__main__":
x=int(raw_input('input number1:'))
y=int(raw_input('input number2:'))
operator=raw_input('input:')
jsq(x,y,operator)
结果如下
# ./sw.py
input number1:11
input number2:22
input:+
33
注意:get()方法是通过键得到值
--结束END--
本文标题: python实现switch语句
本文链接: https://www.lsjlt.com/news/187315.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0