码迷,mamicode.com
首页 > 编程语言 > 详细

Python-if

时间:2014-09-02 17:32:04      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:io   ar   for   cti   sp   on   c   ad   ef   

Python中的switch

>>> choice = ‘ham‘

>>> print({‘spam‘: 1.25,

...     ‘ham‘: 1.99,          # A dictionary-based ‘switch‘

...     ‘eggs‘: 0.99,          # Use has_key or get for default

...     ‘bacon‘: 1.10}[choice])

1.99

>>> branch = {‘spam‘: 1.25,

...       ‘ham‘: 1.99,

...       ‘eggs‘: 0.99}

>>> print(branch.get(‘spam‘, ‘Bad choice‘))

1.25

>>> print(branch.get(‘bacon‘, ‘Bad choice‘))

Bad choice

>>> choice = ‘bacon‘

>>> if choice in branch

...    print(branch[choice])

...  else:

...    print(‘Bad choice‘)

...

Bad choice

===========================================================

 

Python-if

标签:io   ar   for   cti   sp   on   c   ad   ef   

原文地址:http://www.cnblogs.com/yy-is-ing/p/3951784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!