鱼C论坛

 找回密码
 立即注册
查看: 1000|回复: 0

[技术交流] 这个题目有点儿意思

[复制链接]
发表于 2018-7-22 17:37:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 零度非安全 于 2018-7-22 18:18 编辑

这题目没人做吗?我觉得有点意思
3.png
题目大概就是这样,我的解答如下:
  1. '''
  2. 作者: 零度非安全
  3. 环境:Linux
  4. 工具:PyCharm + Anaconda
  5. github: https://github.com/Darkmans
  6. 博客: https://fanfanblog.cn  (评论有糖吃~~)      
  7. '''

  8. import dropdata
  9. import random
  10. import sys

  11. # 游戏菜单
  12. def game_menu():
  13.     print('--------开宝箱游戏--------')
  14.     print('|\t\t选项\t\t\t\t|')
  15.     print('|\t\t1:金宝箱\t\t\t|')
  16.     print('|\t\t2:银宝箱\t\t\t|')
  17.     print('|\t\t3:铜宝箱\t\t\t|')
  18.     print('|\t\t0:退出\t\t\t|')
  19.     print('-------------------------')

  20. # 游戏逻辑处理
  21. def game_logic_handle(input_option):
  22.     # 最大权重初始化
  23.     max_weight = 0
  24.     # 用来存放每种宝箱权重的最大值
  25.     box_list = []
  26.     # 用来存入每个阶段范围
  27.     scope_list = []
  28.     for i in range(1, 4):
  29.         for j in range(0, 5):
  30.             init_weight = max_weight
  31.             max_weight += dropdata.data[i][j]['weight']
  32.             scope_list.append((init_weight + 1, max_weight))
  33.         box_list.append(max_weight)
  34.         max_weight = 0
  35.     # 调用范围比较函数
  36.     scope_compare(input_option, box_list, scope_list)
  37.     return 0

  38. # 范围比较
  39. def scope_compare(input_option, box_list, scope_list):
  40.     # 储存选项相对应的类别宝箱
  41.     box_dict = {
  42.         1: '金',
  43.         2: '银',
  44.         3: '铜'
  45.     }
  46.     # 将每个类别宝箱中的阶段范围单独切出来
  47.     box_scope_list = [scope_list[0:5], scope_list[5:10], scope_list[10:15]]
  48.     # 生成随机数
  49.     box_rd = random.randint(1, box_list[input_option - 1])
  50.     # 打印相应宝箱中的随机数
  51.     print('你在' + box_dict[input_option] + '宝箱中获得的随机数为:', box_rd)
  52.     # 判断随机数在哪个阶段范围,输出对应的itemid值
  53.     if box_rd <= box_scope_list[input_option - 1][0][1]:
  54.         print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option][0]['itemid'])
  55.     if box_scope_list[input_option - 1][1][0] <= box_rd <= box_scope_list[input_option - 1][1][1]:
  56.         print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option][1]['itemid'])
  57.     if box_scope_list[input_option - 1][2][0] <= box_rd <= box_scope_list[input_option - 1][2][1]:
  58.         print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option][2]['itemid'])
  59.     if box_scope_list[input_option - 1][3][0] <= box_rd <= box_scope_list[input_option - 1][3][1]:
  60.         print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option ][3]['itemid'])
  61.     if box_scope_list[input_option - 1][4][0] <= box_rd <= box_scope_list[input_option - 1][4][1]:
  62.         print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option][4]['itemid'])
  63.     return 0

  64. # 主游戏开始
  65. def game_main():
  66.     # 存放选项
  67.     option_list = [0, 1, 2, 3]
  68.     try:
  69.         while True:
  70.             input_option = int(input('请输入符合要求的整数(0~3):'))
  71.             if input_option in option_list:
  72.                 if input_option == 0:
  73.                     print('游戏退出!')
  74.                     sys.exit()
  75.                 else:
  76.                     game_logic_handle(input_option)
  77.                     print('go on 继续 ~~~')
  78.                     game_menu()
  79.                     continue
  80.             else:
  81.                 print('你输入有误')
  82.                 print('go on 继续 ~~~')
  83.                 game_menu()
  84.                 game_main()
  85.     except ValueError:
  86.         print('你输入有误')
  87.         print('go on 继续 ~~~')
  88.         game_menu()
  89.         game_main()

  90. if __name__ == '__main__':
  91.     # 输出游戏菜单
  92.     game_menu()
  93.     # 主游戏开始
  94.     game_main()
复制代码

运行测试后结果如下:
0.png
1.png
2.png
还不错哦~~~~~

神秘之处,戳我
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 06:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表