|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #!/usr/bin/python
- #-*- coding:utf-8 -*-
- import random, dropdata
- def test_drop(drop_id,drop_num):
- list1 = []
- box = [i['itemid'] for i in dropdata.data[drop_id]]
- weight = [i['weight'] for i in dropdata.data[drop_id]]
- for i in range(drop_num):
- r = random.randint(1, sum(weight))
- if r <= weight[0]:
- list1.append(box[0])
- elif r <= sum(weight[:2]):
- list1.append(box[1])
- elif r <= sum(weight[:3]):
- list1.append(box[2])
- elif r <= sum(weight[:4]):
- list1.append(box[3])
- else:
- list1.append(box[4])
- return list1
-
-
-
-
- if __name__ == '__main__':
- while True:
- x = int(input('请输入宝箱种类:1金宝箱,2银宝箱,3铜宝箱,0退出:'))
- if x not in (0, 1, 2, 3):
- print "输入错误,重新输入"
- if x == 0:
- break
- print(test_drop(x, 1))
- 为什么输入除了0.1.2.3时的其他东西是会出现这样报错
- [code]请输入宝箱种类:1金宝箱,2银宝箱,3铜宝箱,0退出:4
- 输入错误,重新输入
- Traceback (most recent call last):
- File "E:\python文件夹\drop.py", line 37, in <module>
- print(test_drop(x, 1))
- File "E:\python文件夹\drop.py", line 11, in test_drop
- box = [i['itemid'] for i in dropdata.data[drop_id]]
- KeyError: 4
复制代码
在32行先不要int(),首先判断input返回的字符串是否为数字(用.isdigit),然后在int()
|
|