switchcase语句python(switchcase函数)

http://www.itjxue.com  2023-01-26 10:32  来源:未知  点击次数: 

Python里怎么实现switch case

python没有swich语句,可以使用if语句替代:

if a == 1:

pass

elif a==2:

pass

else:

pass

python里有没有类似与c语言switch...case...的条件判断语句

有if 、while循环。if elif else 可以实现你说的这个switch。

num = raw_input('please input number :')

if num 0:

print 'num 小于 0'

elif num = 0:

print 'num等于0'

elif num 0:

print 'num 大于0'

else:

print 'num 不是数字 '

大体是这么个意思了,数字判断,要转换类型。

在python中没有switch-case语句

开发者认为switch case语句不好,反正也可以用if else来做干脆就不支持了

为什么Python中没有Switch/Case语句

个人感觉 Python的字典,基本上是可以当着switch语句来使用的,而且用起来比较简便。

Python那些优雅的写法:switch-case

实际上Python是没有所谓的 switch case 写法的,熟悉C艹和Java的同学可能已经习惯了用 switch case 结构去优雅的处理一些事情,比如这样:

但是在Python中,官方对 switch case 的需求是这样回复的:

" You can do this easily enough with a sequence of if... elif... elif... else. There have been some proposals for switch statement syntax, but there is no consensus (yet) on whether and how to do range tests. "

感觉有点low。

可用字典方法解决这个问题:

python中有switch语句吗

python官网的回答(地址:):

You can do this easily enough with a sequence of if... elif... elif... else.

意思就是:python语法讲究简单明了,if else完全可以很简单的实现switch的所有功能,没必要用switch。

(责任编辑:IT教学网)

更多

推荐计算机等级考试文章