鱼C论坛

 找回密码
 立即注册
查看: 2860|回复: 7

怎么引用数据

[复制链接]
发表于 2017-9-28 16:50:28 | 显示全部楼层 |阅读模式

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

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

x
求问,怎么实现输入一个数字,然后就引用到一段数据
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-28 17:13:44 | 显示全部楼层
请描述清楚
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 17:15:04 | 显示全部楼层

假定玩家开宝箱分别有三种类型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中按照对应的权重,打印出一个)
要求:引用dropdata.py的数据
执行drop.py文件后,完成上述流
drop.py文件可以作为模块被代码其它调用,提供如下接口:
test_drop(drop_id,drop_num):根据给定的id,开启次数;返回数组或字典,需要存储获得的itemid及对应的次数
给定奖励表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,},

],

}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 17:15:40 | 显示全部楼层

就是这样
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-28 17:27:20 | 显示全部楼层
sw546190767 发表于 2017-9-28 17:15
假定玩家开宝箱分别有三种类型1金宝箱,2银宝箱,3铜宝箱
金宝箱1 可能开出物品包括 100001,100002,1000 ...

不好意思,你这样的描述我也是看不懂。

你还是用通俗易懂的例子来描述。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-9-28 17:36:56 | 显示全部楼层
ba21 发表于 2017-9-28 17:27
不好意思,你这样的描述我也是看不懂。

你还是用通俗易懂的例子来描述。

这个就是输入数字,0的话就程序退出.1的话,就在1到230之间随机一个数字,然后看那个数字多大,小于11话,就打印10001,小于31大于10的话就打印10002,以此类推。2的话也是在1到230之间随机一个数字,然后也是看数字多大打印什么。3也是,大于3的话就打印输入错误这样子
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-28 21:36:59 | 显示全部楼层
drop.py的代码
  1. #!/usr/bin/python3
  2. # -*- coding:utf-8 -*-
  3. import random
  4. from dropdata import box

  5. def test_drop(drop_id,drop_num):
  6.     mybox = box(drop_id)
  7.     for i in range(drop_num):
  8.         totle = sum(mybox.values())
  9.         random_num = random.randint(1,totle)
  10.         sum1 = 0
  11.         for j in range(1,6):
  12.             index = '%s0000%s'%(drop_id,j)
  13.             sum1 += mybox[index]
  14.             if random_num > sum1:
  15.                 continue
  16.             else:
  17.                 print('itemid:%s'%(index))
  18.                 with open('dropdata.txt','a',encoding='utf-8') as f:
  19.                     f.write('itemid:%s,启动总次数:%s,当前第%s次。\n'%(index,drop_num,i+1))
  20.                 break

  21. while True:
  22.     drop_id = input('请输入数字,输入0退出。')
  23.     if type != 'int':
  24.         try:
  25.             drop_id = int(float(drop_id))
  26.         except:
  27.             print("你输入的类型不是数字,请重新输入。")
  28.             continue
  29.     if drop_id == 0 :
  30.         break
  31.     elif drop_id > 3:
  32.         print("你输入的数字不在范围内,请输入:0,1,2,3")
  33.     else:
  34.         while True:
  35.             try:
  36.                 drop_num = int(float(input("请输出开启的次数:")))
  37.                 if drop_num > 0:
  38.                     break
  39.                 else:
  40.                     continue
  41.             except:
  42.                 print('开启次数也要求输入数字!')
  43.         test_drop(drop_id,drop_num)
复制代码


dropdata.py要引用的数据
  1. #!/usr/bin/python3
  2. # -*- coding:utf-8 -*-

  3. def box(num):
  4.     box1 = {
  5.         '100001':10,
  6.         '100002':20,
  7.         '100003':30,
  8.         '100004':50,
  9.         '100005':90
  10.     }
  11.     box2 = {
  12.         '200001':10,
  13.         '200002':30,
  14.         '200003':50,
  15.         '200004':60,
  16.         '200005':100
  17.     }
  18.     box3 = {
  19.         '300001':10,
  20.         '300002':100,
  21.         '300003':30,
  22.         '300004':60,
  23.         '300005':10
  24.     }
  25.     mybox = [box1,box2,box3]
  26.     return mybox[num-1]
复制代码


最后会生成一个dropdata.txt文件存数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-9-29 14:02:57 | 显示全部楼层
本帖最后由 ba21 于 2017-9-29 14:15 编辑


刚刚弄到别人的代码:

  1. import random, dropdata



  2. def test_drop(drop_id,drop_num):
  3.     list1 = []
  4.     box = [i['itemid'] for i in dropdata.data[drop_id]]
  5.     weight = [i['weight'] for i in dropdata.data[drop_id]]
  6.     for i in range(drop_num):
  7.         r = random.randint(1, sum(weight))
  8.         if r <= weight[0]:
  9.             list1.append(box[0])
  10.         elif r <= sum(weight[:2]):
  11.             list1.append(box[1])
  12.         elif r <= sum(weight[:3]):
  13.             list1.append(box[2])
  14.         elif r <= sum(weight[:4]):
  15.             list1.append(box[3])
  16.         else:
  17.             list1.append(box[4])
  18.     return list1
  19.                      
  20.             
  21.         
  22.    
  23. if __name__ == '__main__':
  24.     while True:
  25.         x = input('请输入宝箱种类:1金宝箱,2银宝箱,3铜宝箱,0退出:')
  26.         
  27.         if x.isdigit():
  28.             x = int(x)
  29.             if x in (0, 1, 2, 3):
  30.                 if x == 0:
  31.                     break            
  32.                 print(test_drop(x, 1))
  33.             else:
  34.                 print("输入错误,重新输入")
  35.         else:
  36.             print("输入错误,重新输入")
复制代码


dropdata.py
  1. data = {

  2. 1:[

  3. {'itemid': '100001','weight': 10,},

  4. {'itemid': '100002','weight': 20,},

  5. {'itemid': '100003','weight': 30,},

  6. {'itemid': '100004','weight': 50,},

  7. {'itemid': '100005','weight': 90,},

  8. ],

  9. 2:[

  10. {'itemid': '200001','weight': 10,},

  11. {'itemid': '200002','weight': 30,},

  12. {'itemid': '200003','weight': 50,},

  13. {'itemid': '200004','weight': 60,},

  14. {'itemid': '200005','weight': 100,},

  15. ],

  16. 3:[

  17. {'itemid': '300001','weight': 10,},

  18. {'itemid': '300002','weight': 100,},

  19. {'itemid': '300003','weight': 30,},

  20. {'itemid': '300004','weight': 60,},

  21. {'itemid': '300005','weight': 10,},

  22. ],

  23. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-12-24 05:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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