python制作抽奖程序代码详解
作者:十一 时间:2022-05-04 08:51:18
实现制作抽奖程序,需要认知到我们可以看到一般抽奖程序界面上是有很多按钮的,比如中奖区域,按键开始区域等等,所以我们先要设置界面,然后把这些按钮添加到界面中去,想必这对于学过tkinter的同学应该不难。下面结合实现步骤:设计界面、利用循环、多线程来完成抽奖程序设置吧。
实现代码:
import random #导入内置的random模块
list1=list(range(0,15)) #将range元素进行列表转换并赋值给列表list1
print("抽奖号码是:",list1) #打印所有的参与抽奖的号码
list2=[] #定义空列表list2,用来储存中奖号码
while len(list1)>0:
result =random.choice(list1) #在列表list1里选择抽取的号码并赋值给result
if result in list1 and result%2==0 and result%3==0:
print("您的号码是:{},恭喜您,您中一等奖".format(result))
list1.remove(result)
list2.append(result)
elif result%5==0:
print("您的号码是:{},恭喜您,您中了二等奖".format(result))
list1.remove(result)
list2.append(result)
elif result%3==0:
print("您的号码是:{},恭喜您,您中了三等奖".format(result))
list1.remove(result)
list2.append(result)
elif result%2!=0 and result%3!=0 and result%5!=0:
print("您的号码是:{},您未中奖".format(result))
elif result==list1[-1] or result==list1[0]: #当抽取到列表list1最后一个或者第一个元素时
print("您的号码是:{},抽奖结束".format(result)) #打印号码,并打印抽奖结束
print("中奖名单是:", list2)
print("未中奖名单是:", list1)
Break
输出结果:
抽奖号码是: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
您的号码是:5,恭喜您,您中了二等奖
您的号码是:10,恭喜您,您中了二等奖
您的号码是:6,恭喜您,您中一等奖
您的号码是:3,恭喜您,您中了三等奖
您的号码是:13,您未中奖
您的号码是:11,您未中奖
您的号码是:14,抽奖结束
中奖名单是: [5, 10, 6, 3]
未中奖名单是: [0, 1, 2, 4, 7, 8, 9, 11, 12, 13, 14]
实例扩展:
import xlrd,random#导入读取excel的模块xlrd,取随机数的模块random
data = xlrd.open_workbook("01.xls")#xlrd模块中的函数
table = data.sheet_by_index(0)#同上
num = input("请输入抽奖人数:")
start = input("请输入起始位置:")
end = input("请输入结束位置:")
start = eval(start)#将字符串转换为整数
end = eval(end)
num = eval(num)
print("获奖名单为:")
for i in range(num):
idx = random.randint(start,end+1)
tmp = table.cell_value(idx,1)#将读取到的元素临时存储到tmp中
print(tmp)#输出tmp中存储的值
来源:https://www.py.cn/jishu/gaoji/22971.html
标签:python,抽奖程序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
微软补丁KB967723造成MySQL频繁无法连接
2009-11-03 14:28:00
mysql查询一天,查询一周,查询一个月的数据
2011-01-29 16:22:00
python字符串连接的N种方式总结
2023-10-12 08:47:44
Python文件处理与垃圾回收机制详情
2023-06-14 16:36:17
帮你六步改善SQL Server安全规划全攻略
2009-01-20 15:07:00
Appium自动化测试实现H5页面元素定位
2023-06-22 02:25:53
![](https://img.aspxhome.com/file/2023/0/60740_0s.png)
PHP简单检测网址是否能够正常打开的方法
2023-11-17 22:22:56
![](https://img.aspxhome.com/file/2023/7/104187_0s.jpg)
apache集成php5.6方法分享
2023-09-06 09:03:54
![](https://img.aspxhome.com/file/2023/7/55477_0s.png)
如何把外网python虚拟环境迁移到内网
2021-12-28 14:08:13
关于交互设计在QQ秀赠送流程中的优化
2009-06-11 12:30:00
如何避免查询调查结果时出现不相关主题的重复记录?
2009-11-07 18:42:00
asp如何显示全部的环境变量?
2010-06-08 09:34:00
PHP实现多文件上传的方法
2023-11-23 03:42:04
Pivot,信息组织的梦想之窗
2009-12-25 14:32:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/pivot-01-87s.jpg)
编码问题引起的折腾
2009-07-03 12:43:00
python编程控制Android手机操作技巧示例
2021-12-01 07:59:05
![](https://img.aspxhome.com/file/2023/3/69553_0s.png)
40个网页设计常用小代码
2008-01-01 19:27:00
Go语言基于Socket编写服务器端与客户端通信的实例
2023-07-12 08:44:11
![](https://img.aspxhome.com/file/2023/3/135453_0s.png)
使用Python的Bottle框架写一个简单的服务接口的示例
2023-09-01 17:29:50
关于设计的一些个人看法
2008-06-12 12:44:00