鱼C论坛

 找回密码
 立即注册
查看: 924|回复: 4

[已解决]求帮忙用python2.7版本写一个作业,求救,急着交,写得好的私信红包,谢谢

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

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

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

x
本帖最后由 jluzhpanda 于 2018-7-22 00:12 编辑

求帮忙用python2.7版本写一个作业,本人初学,实在写不出,求救,急着交
最好注释一下代码的意思,尽量用初学能看得懂的代码,不要用对象,实例之类的看不太懂。。谢谢大佬们

假定玩家开宝箱分别有三种类型1金宝箱,2银宝箱,3铜宝箱
        金宝箱1 可能开出物品包括 100001,100002,100003,100004,100005,它们分别的权重为10,20,30,50,90;
        银宝箱2 可能开出物品包括 200001,200002,200003,200004,200005,它们分别的权重为10,30,50,60,100;
        铜宝箱3 可能开出物品包括 300001,300002,300003,300004,300005,它们分别的权重为10,100,30,60,10;
    开宝箱根据权重获得对应的物品,如开1次金宝箱,流程可以如下:从1-总权重(10+20+30+50+90)中随机一个数,如果这个数处于1-10,则返回100001,处于(10+1)-(10+20)则返回100002,处于(1+10+20)-(10+20+30)则返回100003。。。类似。

作业流程:
        执行drop.py文件开始。
        代码接受玩家输入,提示玩家可以输入0,1,2,3(一直接收玩家输入,直到玩家输入0退出)
        如果玩家输入0,则程序退出
        如果玩家输入1,2,3中的某个数,按照上述给出概率打出对应的物品id(如玩家输入1,则在100001-100005中按照对应的权重,打印出一个)
请写一个简单的代码,命名为drop.py
        引用dropdata.py的数据
        执行drop.py文件后,完成上述流程
        drop.py文件可以作为模块被代码其它调用,提供如下接口:
            get_drop_item(drop_id):根据给定的id,如果dropdata的data中存在,则根据权重返回一个itemid
            test_drop(drop_id,drop_num):根据给定的id,开启次数;返回数组或字典,需要存储获得的itemid及对应的次数

下面是已给定的2个py文件

*********test_drop.py********

import drop

def test():
    for drop_id in [1,2,3]:
        print "drop_id:%s"%drop_id
        print "get_drop_item:",drop.get_drop_item(drop_id)
        print "test_drop:",drop.test_drop(drop_id,100)

if __name__ =="__main__":
    test()



*********dropdata.py*********

data = {
    1:[
            {'itemid': '100001','weight': 10,},
        {'itemid': '100002','weight': 20,},
        {'itemid': '100003','weight': 30,},
        {'itemid': '100004','weight': 50,},
        {'itemid': '100005','weight': 90,},
],

    2:[
            {'itemid': '200001','weight': 10,},
        {'itemid': '200002','weight': 30,},
        {'itemid': '200003','weight': 50,},
        {'itemid': '200004','weight': 60,},
        {'itemid': '200005','weight': 100,},
],

    3:[
            {'itemid': '300001','weight': 10,},
        {'itemid': '300002','weight': 100,},
        {'itemid': '300003','weight': 30,},
        {'itemid': '300004','weight': 60,},
        {'itemid': '300005','weight': 10,},
],
}
最佳答案
2018-7-22 17:32:17
本帖最后由 零度非安全 于 2018-7-22 18:20 编辑

具体流程度我已帮你全部实现了,有时间可以多访问下我的博客,https://fanfanblog.cn

代码如下(你要求的那两个接口我没写,你自己稍微思考下就可以写出,另外是 Python3 ):
  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
回复

使用道具 举报

发表于 2018-7-22 17:32:17 | 显示全部楼层    本楼为最佳答案   
本帖最后由 零度非安全 于 2018-7-22 18:20 编辑

具体流程度我已帮你全部实现了,有时间可以多访问下我的博客,https://fanfanblog.cn

代码如下(你要求的那两个接口我没写,你自己稍微思考下就可以写出,另外是 Python3 ):
  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
回复 支持 反对

使用道具 举报

发表于 2018-7-22 19:35:04 | 显示全部楼层
本帖最后由 凌九霄 于 2018-7-22 19:37 编辑
  1. import random


  2. def tbox(num, data):
  3.     wlst = [ x[ 'weight' ] for x in data[ num ] ]
  4.     randomweight = random.randint(1, sum(wlst))
  5.     nwlst = [ sum(wlst[ :i ]) + 1 for i in range(1, len(wlst) + 1) ]
  6.     tmp = nwlst[ : ]
  7.     tmp.append(randomweight)
  8.     tmp = sorted(tmp)

  9.     return data[ num ][ nwlst.index(tmp[ tmp.index(randomweight) + 1 ]) ][ 'itemid' ]


  10. print(tbox(1, data))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-23 01:10:05 | 显示全部楼层
谢谢大佬,已经去你博客了,还加了你微信公众号了
不过好像没使用到test_drop这个文件啊,
等我慢慢消化你写的,再自己试试。。非常感谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-7-23 01:12:02 | 显示全部楼层

这位大佬,没入选最佳,不过还是非常感谢你
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 00:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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