自己动手做一个抽奖小应用遇到一点问题
#38讲课后作业,想写一个抽奖程序,并且是根据权重抽奖的,还要有倒计时。#随机:random 窗口:easygui
import random as r
import easygui as g
g.msgbox(msg='欢迎使用W1ND抽奖程序',title='随机抽奖')
def test():
global Name_dict
s_sum = sum(Name_dict.values())
h = r.randint(0,s_sum)
c_sum = 0
for k in Name_dict.key():
c_sum += Name_dict
if c_sum > h :
return k
#存储名字和所占的比重,昨晚想到了解决办法,就是在文档里输入【名字+ : +权重】,然后在读取
start_dir = g.fileopenbox(msg='请选择你已经整理好的抽奖名单',title='抽奖文件选择',default='.txt')
with open(start_dir) as f:
Name_dict = {}
for i in f:
(a,b) = i.split(':',1)
c_num = int(b)
Name = a
rate = c_num
try: #用字典的形式进行权重统计
Name_dict += rate
except KeyError:
Name_dict = rate
g.msgbox(msg='权重统计完成')
g.msgbox(msg='参加抽奖的人数有: %d 个' % len(Name_dict))
#至此全部统计完成,并且以字典的形式储存在Name_dict中
result = dict([(x, 0) for x in Name_dict.keys()])# 这一步简直太厉害了!!!
kt = test()
result += 1
print(result) Traceback (most recent call last):
File "E:\py\小甲鱼课后作业\038\自己设计第一个抽奖程序.py", line 43, in <module>
kt = test()
File "E:\py\小甲鱼课后作业\038\自己设计第一个抽奖程序.py", line 17, in test
for k in Name_dict.key():
AttributeError: 'dict' object has no attribute 'key'
报错了,希望有大神帮我检查一下代码呜呜呜 keys 真糊涂了{:10_244:}
页:
[1]