|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 零度非安全 于 2018-7-22 18:18 编辑
这题目没人做吗?我觉得有点意思
题目大概就是这样,我的解答如下:
- '''
- 作者: 零度非安全
- 环境:Linux
- 工具:PyCharm + Anaconda
- github: https://github.com/Darkmans
- 博客: https://fanfanblog.cn (评论有糖吃~~)
- '''
- import dropdata
- import random
- import sys
- # 游戏菜单
- def game_menu():
- print('--------开宝箱游戏--------')
- print('|\t\t选项\t\t\t\t|')
- print('|\t\t1:金宝箱\t\t\t|')
- print('|\t\t2:银宝箱\t\t\t|')
- print('|\t\t3:铜宝箱\t\t\t|')
- print('|\t\t0:退出\t\t\t|')
- print('-------------------------')
- # 游戏逻辑处理
- def game_logic_handle(input_option):
- # 最大权重初始化
- max_weight = 0
- # 用来存放每种宝箱权重的最大值
- box_list = []
- # 用来存入每个阶段范围
- scope_list = []
- for i in range(1, 4):
- for j in range(0, 5):
- init_weight = max_weight
- max_weight += dropdata.data[i][j]['weight']
- scope_list.append((init_weight + 1, max_weight))
- box_list.append(max_weight)
- max_weight = 0
- # 调用范围比较函数
- scope_compare(input_option, box_list, scope_list)
- return 0
- # 范围比较
- def scope_compare(input_option, box_list, scope_list):
- # 储存选项相对应的类别宝箱
- box_dict = {
- 1: '金',
- 2: '银',
- 3: '铜'
- }
- # 将每个类别宝箱中的阶段范围单独切出来
- box_scope_list = [scope_list[0:5], scope_list[5:10], scope_list[10:15]]
- # 生成随机数
- box_rd = random.randint(1, box_list[input_option - 1])
- # 打印相应宝箱中的随机数
- print('你在' + box_dict[input_option] + '宝箱中获得的随机数为:', box_rd)
- # 判断随机数在哪个阶段范围,输出对应的itemid值
- if box_rd <= box_scope_list[input_option - 1][0][1]:
- print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option][0]['itemid'])
- if box_scope_list[input_option - 1][1][0] <= box_rd <= box_scope_list[input_option - 1][1][1]:
- print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option][1]['itemid'])
- if box_scope_list[input_option - 1][2][0] <= box_rd <= box_scope_list[input_option - 1][2][1]:
- print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option][2]['itemid'])
- if box_scope_list[input_option - 1][3][0] <= box_rd <= box_scope_list[input_option - 1][3][1]:
- print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option ][3]['itemid'])
- if box_scope_list[input_option - 1][4][0] <= box_rd <= box_scope_list[input_option - 1][4][1]:
- print('恭喜你获得' + box_dict[input_option] + '宝箱中的物品:', dropdata.data[input_option][4]['itemid'])
- return 0
- # 主游戏开始
- def game_main():
- # 存放选项
- option_list = [0, 1, 2, 3]
- try:
- while True:
- input_option = int(input('请输入符合要求的整数(0~3):'))
- if input_option in option_list:
- if input_option == 0:
- print('游戏退出!')
- sys.exit()
- else:
- game_logic_handle(input_option)
- print('go on 继续 ~~~')
- game_menu()
- continue
- else:
- print('你输入有误')
- print('go on 继续 ~~~')
- game_menu()
- game_main()
- except ValueError:
- print('你输入有误')
- print('go on 继续 ~~~')
- game_menu()
- game_main()
- if __name__ == '__main__':
- # 输出游戏菜单
- game_menu()
- # 主游戏开始
- game_main()
复制代码
运行测试后结果如下:
还不错哦~~~~~
神秘之处,戳我
|
|