python 字典实现类似c的switch case
#python 字典实现类似c的switch def print_hi():
print('hi') def print_hello():
print('hello') def print_goodbye():
print('goodbye') choice = int(input('please input your choice:')) # 例子,不考虑输入错误的情况 # if ... elif 实现
if choice ==1:
print_hi()
elif choice ==2:
print_hello()
elif choice ==3:
print_goodbye() # 字典实现
choice_dict = {1:print_hi, 2:print_hello, 3:print_goodbye} # 这里只是引用函数,如果写成print_hi()这种形式,则一运行程序,所有选择都会执行一遍
# 替代方案是:1:lamba:print_hi()这种形式
choice = int(input('please input your choice:')) choice_dict[choice]() # 当对一函数引用但不加()时,只是引用,并不执行,所以这里加上()如果有参数,也可以传参数
python 字典实现类似c的switch case的更多相关文章
- Python里如何实现C中switch...case的功能
python没有switch case 不过可以通过建立字典实现类似的功能 例子:根据输入的年月日,判断是该年中的第几天 y = int(input('请输入年:')) m = int(input(' ...
- Thymeleaf常用语法:条件判断 if、switch case
if语句条件判断使用th:if,它会判断表达式是否成立,表达式的结果支持boolean.number.character.String及其他类型.满足下面情况,if语句成立:(1) 表达式的结果是数字 ...
- python之 利用字典与函数实现switch case功能
Python不像C/C++,Java等有switch-case的语法.不过其这个功能,比如用Dictionary以及lambda匿名函数特性来替代实现. 字典+函数实现switch模式下的四则运算:( ...
- Python | 基础系列 · Python为什么没有switch/case语句?
与我之前使用的所有语言都不同,Python没有switch/case语句.为了达到这种分支语句的效果,一般方法是使用字典映射: def numbers_to_strings(argument): sw ...
- python中Switch/Case实现
学习Python过程中,发现没有switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch/Case功能. 方法一 通过字典实现 ...
- Why Doesn't Python Have Switch/Case?
Why Doesn't Python Have Switch/Case? Tuesday, June 09, 2015 (permalink) Unlike every other programmi ...
- python技巧 switch case语句
不同于C语言和SHELL,python中没有switch case语句,关于为什么没有,官方的解释是这样的 使用Python模拟实现的方法: def switch_if(fun, x, y): ...
- python的字典有些类似js对象
python的字典有些类似js对象 dict1 = {} dict1['one']= '1-one' dict1[2] = '2-tow' tinydict = {'name':'tome','cod ...
- Python字典(dict)使用技巧
字典dict是Python中使用频率非常高的数据结构,关于它的使用,也有许多的小技巧,掌握这些小技巧会让你高效地的使用dict,也会让你的代码更简洁. 1.默认值 假设name_for_userid存 ...
随机推荐
- LINQ To SQL 语法及实例大全
http://blog.csdn.net/pan_junbiao/article/details/7015633 http://blog.csdn.net/pan_junbiao/article/de ...
- windows p12(pfx)个人证书安装过程
证书库个人证书存储区为其中的每个证书维护一个属性CERT_KEY_PROV_INFO_PROP_ID,该属性指定了证书对应的密钥容器的相关信息,包括密钥容器名,CSP名称,CSP类型,密钥用途,以及C ...
- [转]开源那些事儿(四)-如何使用CodePlex进行项目管理
本文版权信息作者:Jake Lin(Jake's Blog on 博客园) 出处:http://www.cnblogs.com/procoder/archive/2010/02/10/About-Op ...
- Javascript 图片左右滑动与切换
Html代码 : <div class="v_out v_out_p"> <div class="prev"> <a href=& ...
- MySQL错误: could not retrieve transation read-only status server
问题描述: java代码在开始事务后,先做了一个查询,再insert,此时会报: java.sql.SQLException: could not retrieve transati ...
- MySQL存储引擎【InnoDB、MyISAM、Memory】
数据库,MySQL这样存在多存储引擎的数据库软件,清楚常见的存储引擎的区别,使用合适的存储引擎,使得项目跑的更顺畅,有时候对于一个项目,甚至比项目本身都重要.这篇文章,旨在浅谈常见的三种存储引擎的区别 ...
- Enable EPEL Repository for RHEL/CentOS 7.x/6.x/5.x
This howto guide shows you’ll how to enable EPEL repository under RHEL/CentOS 6/5 to install additio ...
- RegisterClientScriptBlock 与 RegisterStartupScript 的区别
RegisterClientScriptBlock.RegisterStartupScript 都是注册一个脚本块. ClientScript.RegisterClientScriptBlock(th ...
- div里面的内容超出自身高度时,显示省略号
1.给DIV设置属性:width: 200px; text-overflow: ellipsis; overflow: hidden; 当div里面的内容总宽度找过 200PX的时候,超出的部分会以“ ...
- PL/SQL中查看列注释、列属性