【Python练习】抽奖小程序

1.做个抽奖程序,可以输入一个人的名字和抽奖号,然后随机抽取存在的抽奖号,程序可以指定抽取次数,抽取后显示抽奖号和名字,全部抽取完成后输出抽奖的总结果

算法:
1 能够死循环读入不同的名字和抽奖号,使用字典保存,在输入done的时候退出
死循环。
2 输入抽奖的次数,保存到一个变量中。
3 使用random.shuffle或者choice来随机抽奖抽奖,使用for循环抽取设定的次数,
4 使用新的字典变量保存抽奖的结果,并输出。

import random
pool={}
while 1:
    user_input_content=raw_input("please input No and name,sep by ,:")
    if user_input_content.lower()=="done":break
    no,name=user_input_content.split(",")
    pool[no]=name
print pool

while 1:
    try:
        lotte_times=int(raw_input("please input lotter times:"))
    except:
        print "please input a int number bigger than 0,try again."
        continue
    if lotte_times>0:
        break
    else:
        print "please input a int number bigger than 0,try again."  
print "lotte times:",lotte_times

result={}
for i in range(lotte_times):
    r=random.choice(pool.keys())
    result[r]=pool[r]
    del pool[r]

print result.values()

猜你喜欢

转载自blog.csdn.net/qq_30758629/article/details/80841421