Chinaunix首页 | 论坛 | 博客
  • 博客访问: 901868
  • 博文数量: 285
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 2881
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
  • 认证徽章:
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(285)

文章存档

2019年(13)

2018年(43)

2017年(79)

2016年(80)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2019-02-23 17:39:19

使用python中的字典来模拟其他语言中的case,switch 语句。
使用函数作为字典的value. 看代码很直观。
该代码出自The Python Master一书.

点击(此处)折叠或打开

  1. def labyrinth(position, alive):
  2.     print('You are in a maze of twisty passages, all alike.')
  3.     return position, alive

  4. def dark_forest_road(position, alive):
  5.     print("You are on a road in a dark forest. To"
  6.     " The north you can see a tower.")
  7.     return position, alive
  8.     
  9. def tall_tower(position, alive):
  10.     print("There is a tall tower here, with no obvious door. "
  11.     "A path leads east.")
  12.     return position, alive

  13. def rabbit_hole(position, alive):
  14.     print("You fall down a rabbit hole into a labyrinth.")
  15.     return (0,0), alive

  16. def lava_pit(position, alive):
  17.     print("You fall into a lava pit.")
  18.     return position, False

  19. def go_north(position):
  20.     i, j = position
  21.     new_position = (i, j + 1)
  22.     return new_position

  23. def go_east(position):
  24.     i, j = position
  25.     new_position = (i + 1, j)
  26.     return new_position

  27. def go_south(position):
  28.     i, j = position
  29.     new_position = (i, j - 1)
  30.     return new_position

  31. def go_west(position):
  32.     i, j = position
  33.     new_position = (i - 1, j)
  34.     return new_position

  35. def look(position):
  36.     return position

  37. def quit(position):
  38.     return None

  39. locations = {
  40.     (0, 0): labyrinth,
  41.     (1, 0): dark_forest_road,
  42.     (1, 1): tall_tower,
  43.     (2, 1): rabbit_hole,
  44.     (1, 2): lava_pit,
  45.     }
  46.     
  47. actions = {
  48.     'N': go_north,
  49.     'E': go_east,
  50.     'S': go_south,
  51.     'W': go_west,
  52.     'L': look,
  53.     'Q': quit,
  54.     }



  55. def play():
  56.     
  57.     position = (0, 0)
  58.     alive = True
  59.     
  60.     while position:
  61.         
  62.         try:
  63.             location_action = locations[position]
  64.         except KeyError:
  65.             print("There is nothing here.")
  66.         else:
  67.             position, alive = location_action(position, alive)
  68.             if not alive:
  69.                 print("You're dead")
  70.                 break
  71.         
  72.         command = input()
  73.         
  74.         try:
  75.             command_action = actions[command]
  76.         except KeyError:
  77.             print("I don't understand")
  78.         else:
  79.             position = command_action(position)
  80.         

  81.     else: #nobreak
  82.         print("You have chosen to leave the game")
  83.     
  84.     print("Game Over")
  85.     
  86. play()

执行起来结果如下:

点击(此处)折叠或打开

  1. root@kali:/usr/local/src/py/project_dirs# ./emulate_switch_3.py
    You are in a maze of twisty passages, all alike.
    E
    You are on a road in a dark forest. To The north you can see a tower.
    N
    There is a tall tower here, with no obvious door. A path leads east.
    N
    You fall into a lava pit.
    You're dead
    Game Over
    root@kali:/usr/local/src/py/project_dirs# ./emulate_switch_3.py
    You are in a maze of twisty passages, all alike.
    E
    You are on a road in a dark forest. To The north you can see a tower.
    N
    There is a tall tower here, with no obvious door. A path leads east.
    E
    You fall down a rabbit hole into a labyrinth.
    E
    You are on a road in a dark forest. To The north you can see a tower.
    N
    There is a tall tower here, with no obvious door. A path leads east.
    E
    You fall down a rabbit hole into a labyrinth.
    Q
    You have chosen to leave the game
    Game Over




阅读(49352) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册