|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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 18:20 编辑
具体流程度我已帮你全部实现了,有时间可以多访问下我的博客, https://fanfanblog.cn
代码如下(你要求的那两个接口我没写,你自己稍微思考下就可以写出,另外是 Python3 ):
- '''
- 作者: 零度非安全
- 环境: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()
复制代码
运行后测试如下:
关于红包,这个我就不要了,你只要在我博客多评论留言说几句话就行~~~~~~
|
|