16年年会抽奖网上有人对公司的抽奖结果又偏见,于是全员进行了抽奖代码的review,好像是爱奇艺公司的,下面用python来实现一个抽奖程序。
主要功能有
1从一个csv文件中读入所有员工工号
2将这些工号初始到一个列表中
3用random模块下的choice函数来随机选择列表中的一个工号
4抽到的奖项的工号要从列表中进行删除,以免再次抽到
初级版
这个比较简单,缺少定制性,如没法设置一等奖有几名,二等奖有几名
import csv#创建一个员工列表emplist = []#用with自动关闭文件with open('c://empscsv') as f:
empf = csvreader(f) for emp in empf:
emplistappend(emp)
print("进行一等奖抽奖,共有一名")import random#利用random模块的chice函数来从列表中随机选取一个元素e1 = randomchoice(emplist)#将中奖的员工从列表中剔除emplistremove(e1)
print('一等奖得主的号码是 %s' % e1)
print('进行三个二等奖的号码抽奖')
e2_1 = randomchoice(emplist)
emplistremove(e2_1)
e2_2 = randomchoice(emplist)
emplistremove(e2_2)
e2_3 = randomchoice(emplist)
emplistremove(e2_3)
print('获得3个二等奖是 %s %s %s',(e2_1,e2_2,e2_3))#下面依次类推可以设置三等奖的抽奖123456789101112131415161718192021222324
改进版
上面的那个初级版,假如要设置个三等奖一百名那么将要重新维护几百行代码,下面用比较高级点的办法实现
我们考虑用面向对象来实现,设计一个抽奖类,类中包含一个属性(号码来源),一个方法:产生所有抽奖层次指定个数的抽奖号码。
用到如下知识点:
1 csv模块部分函数用法
2 sys模块读取输入
3 random模块函数choice函数用法
4 列表和字典元素的添加、删除
6 for循环中range用法
7 类和面向对象
8 字符打印,print中的计算
9open中with
#!/usr/bin/python#coding=utf-8import csvimport sysimport random
reload(sys)
syssetdefaultencoding('utf8')#coding=utf-8print("开始进行抽奖")#定义个抽奖类,功能有输入抽奖级别和个数,打印出每个级别的抽奖员工号码class Choujiang:
#定义scv文件路径
def __init__(self,filepath):
selfempfile = filepath def creat_num(self):
emplist = [] with open(selfempfile) as f:
empf = csvreader(f) for emp in empf:
emplistappend(emp)
print('共有%s 人参与抽奖' % len(emplist))
levels = int(input('抽奖分几个层次,请输入:')) #定义一个字典
level_dict = {} for i in range(0,levels):
print('请输入当前获奖层次 %s 对应的奖品个数' % ( i + 1))
str_level_dict_key = sysstdinreadline()
int_level_dict_key = int(str_level_dict_key)
level_dict[i] = int_level_dict_key #循环完成后抽奖层次字典构造完毕
#进行抽奖开始
print('抽奖字典设置为: %s' % level_dict) for i in range(0,len(level_dict)):
winers = [] #产生当前抽奖层次i对应的抽奖个数
for j in range(0,int(level_dict[i])): #利用random模块中的choice函数从列表中随机产生一个
winer = randomchoice(emplist)
winersappend(winer)
emplistremove(winer)
print('抽奖层次 %s 下产出的获奖人员有:' % (i + 1 ))
print(winers)#类功能定义完毕,开始初始化并使用if __name__ == '__main__':
peoples = Choujiang('c://empscsv')
peoplescreat_num()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
该段程序在python 26 以上及 3中均可以运行,运行结果如下图:
Python 343 (v343:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information>>> ================================ RESTART ================================>>> 开始进行抽奖
共有24790 人参与抽奖
抽奖分几个层次,请输入:2请输入当前获奖层次 1 对应的奖品个数1请输入当前获奖层次 2 对应的奖品个数3抽奖字典设置为: {0: 1, 1: 3}
抽奖层次 1 下产出的获奖人员有:
[['张三19826']]
抽奖层次 2 下产出的获奖人员有:
[['张三18670'], ['张三23235'], ['张三15705']]>>> 1234567891011121314151617
在窗体中放入label、button、timer、memo 四种类型的控件各一个:
label1--用于显示手机号码的滚动
timer1(时钟)--用于产生随机的手机号码
button1--点击即抽奖,并将获奖号码保存到memo1中
memo1--用于保存获奖手机号码
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Timer1: TTimer;
Memo1: TMemo;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
n=20;
a:array[1n] of string[11]=('13607193000','13607193001',
'13607193002','13607193003','13607193004',
'13607193005','13607193006','13607193007',
'13607193008','13607193009',
'13607193010','13607193011','13607193012',
'13607193013','13607193014',
'13607193015','13607193016','13607193017',
'13607193018','13607193019');
var
Form1: TForm1;
implementation
{$R dfm}
procedure TForm1Timer1Timer(Sender: TObject);
var
k:integer;
begin
k:=trunc(nrandom+1);
label1Caption:=a[k];
end;
procedure TForm1FormCreate(Sender: TObject);
begin
randomize;
end;
procedure TForm1Button1Click(Sender: TObject);
begin
memo1LinesAdd(label1Caption);
end;
end
真不好玩,写好还真不简单~
中意的话,多加点分!测试过了~
/
预先将参加抽奖者的资料输入存储起来,主要用结构体数组实现,用结构体表示每项记录,包括身份z号码、姓名、电话、住址等信息。开始抽奖时,屏幕上快速闪动参与者的身份z号码,按任意键停止闪动,把中奖人的信息用显示在屏幕上。可反复抽出一、二、三等奖若干名。
设计实现以下功能:
(1)录入:完成抽奖者信息的输入;
(2)删除:完成抽奖者信息的删除;
(3)修改:允许对已经录入的数据重新进行编辑、修改;
(4)显示:显示所有中奖者的信息;
(5)抽奖:
a 抽出一等奖 1 名,如果抽过就不能再抽;
b 抽出二等奖 2 名,如果抽完就不能再抽;
c 抽出三等奖 5 名,如果抽完就不能再抽;
(6)退出程序。/
#include<stdioh>
#include <conioh>
#include <stdlibh>
#include <memoryh>
#include <stringh>
#include <timeh>
#define AWARDER_NUM 10
typedef struct tag_award
{
char ID[20];
char name[20];
char telNo[20];
char address[50];
}AWARDER;
AWARDER awarder = new AWARDER[AWARDER_NUM];
int first_award[1] = {-1};
int second_award[2] = {-1,-1};
int third_award[5] ={-1,-1,-1,-1,-1};
int awarder_num = 0;
void info_login()
{
if(awarder_num >= AWARDER_NUM)
{
AWARDER new_awarder = new AWARDER[awarder_num + AWARDER_NUM];
memcpy(new_awarder,awarder,awarder_numsizeof(AWARDER));
delete awarder;
awarder = new_awarder;
}
printf("身份z号:");
scanf("%s",awarder[awarder_num]ID);
printf("姓名:");
scanf("%s",awarder[awarder_num]name);
printf("电话号码:");
scanf("%s",awarder[awarder_num]telNo);
printf("家庭住址:");
scanf("%s",awarder[awarder_num]address);
awarder_num++;
}
void info_delete()
{
char name[20];
printf("输入您要删除的抽奖者的姓名:");
scanf("%s",name);
for(int i=0;i<awarder_num;i++)
{
if (strcmp(awarder[i]name,name) == 0)
{
printf("您选择删除的抽奖者的信息如下:\n");
printf("身份z号:%s\n",awarder[i]ID);
printf("姓名:%s\n",awarder[i]name);
printf("电话号码:%s\n",awarder[i]telNo);
printf("家庭住址:%s\n",awarder[i]address);
break;
}
}
if(i == awarder_num)
{
printf("没有找到您输入的抽奖者!\n");
}
else
{
char c;
printf("您确定要删除吗?(Y//N)");
fflush(stdin);
scanf("%c",&c);
if(c == 'Y' || c == 'y')
{
AWARDER new_award = new AWARDER[awarder_num-1];
if(i <= 0)
memcpy(new_award,awarder+1,sizeof(AWARDER) (awarder_num - 1));
else if( i >= awarder_num -1)
memcpy(new_award,awarder,sizeof(AWARDER) (awarder_num - 1));
else
{
memcpy(new_award,awarder,sizeof(AWARDER) i);
memcpy(new_award+i,awarder+i+1,sizeof(AWARDER) (awarder_num - i - 1));
}
awarder_num--;
delete awarder;
awarder = new_award;
}
}
}
void info_modify()
{
char name[20];
printf("输入您要修改的抽奖者的姓名:");
scanf("%s",name);
for(int i=0;i<awarder_num;i++)
{
if (strcmp(awarder[i]name,name) == 0)
{
printf("您选择的抽奖者的信息如下:\n");
printf("身份z号:%s\n",awarder[i]ID);
printf("姓名:%s\n",awarder[i]name);
printf("电话号码:%s\n",awarder[i]telNo);
printf("家庭住址:%s\n",awarder[i]address);
break;
}
}
if(i == awarder_num)
{
printf("没有找到您输入的抽奖者!\n");
}
else
{
printf("请输入该抽奖者的新信息:\n");
printf("身份z号:");
scanf("%s",awarder[i]ID);
printf("姓名:");
scanf("%s",awarder[i]name);
printf("电话号码:");
scanf("%s",awarder[i]telNo);
printf("家庭住址:");
scanf("%s",awarder[i]address);
}
}
bool check_over(int cur_award,int awarderNo)
{
if(awarderNo == 1)
{
if(cur_award == first_award[0])
return true;
}
else if(awarderNo <= 3)
{
if(cur_award == first_award[0])
return true;
for(int i=0;i<awarderNo-1;i++)
if(cur_award == second_award[i])
return true;
}
else if(awarderNo <= 8)
{
if(cur_award == first_award[0])
return true;
for(int i=0;i<2;i++)
if(cur_award == second_award[i])
return true;
for(i=0;i<awarderNo-3;i++)
if(cur_award == third_award[i])
return true;
}
return false;
}
void award()
{
if(awarder_num < 8)
{
printf("人数太少,请添加足够的人再抽奖!\n");
return;
}
srand((long)time(0));
char hit_id[20];
printf("\n开始抽第一等奖(1名),按任意键开始\n");
getch();
while(!kbhit())
{
first_award[0] = rand() % awarder_num;
memcpy(hit_id,awarder[first_award[0]]ID,18);
printf("\r ");
printf("\r %s",hit_id);
}
printf("\n\n开始抽第二等奖(2名)");
for (int i=0;i<2;i++)
{
printf("\n第%d个二等奖,按任意键开始\n",i+1);
getch();
while(!kbhit())
{
second_award[i] = rand() % awarder_num;
while(check_over(second_award[i],1+i))
second_award[i] = rand() % awarder_num;
memcpy(hit_id,awarder[second_award[i]]ID,18);
printf("\r ");
printf("\r %s",hit_id);
}
}
printf("\n\n开始抽第三等奖(5名)");
for (i=0;i<5;i++)
{
printf("\n第%d个三等奖,按任意键开始\n",i+1);
getch();
while(!kbhit())
{
third_award[i] = rand() % awarder_num;
while(check_over(third_award[i],3+i))
third_award[i] = rand() % awarder_num;
memcpy(hit_id,awarder[third_award[i]]ID,20);
printf("\r ");
printf("\r %s",hit_id);
}
}
}
void info_show()
{
printf("一等奖获得者:\n");
printf("\t身份z号:%s\n",awarder[first_award[0]]ID);
printf("\t姓名:%s\n",awarder[first_award[0]]name);
printf("\t电话号码:%s\n",awarder[first_award[0]]telNo);
printf("\t家庭住址:%s\n",awarder[first_award[0]]address);
printf("二等奖获得者:\n");
for (int i=0;i<2;i++)
{
printf("第%d个二等奖获得者:\n");
printf("\t身份z号:%s\n",awarder[second_award[i]]ID);
printf("\t姓名:%s\n",awarder[second_award[i]]name);
printf("\t电话号码:%s\n",awarder[second_award[i]]telNo);
printf("\t家庭住址:%s\n",awarder[second_award[i]]address);
}
printf("三等奖获得者:\n");
for (int i=0;i<5;i++)
{
printf("第%d个三等奖获得者:\n");
printf("\t身份z号:%s\n",awarder[third_award[i]]ID);
printf("\t姓名:%s\n",awarder[third_award[i]]name);
printf("\t电话号码:%s\n",awarder[third_award[i]]telNo);
printf("\t家庭住址:%s\n",awarder[third_award[i]]address);
}
}
/提示信息/
bool raise()
{
printf("\n请输入你需要的 *** 作代号:\n");
printf("\t1录入:完成抽奖者信息的输入;\n");
printf("\t2删除:完成抽奖者信息的删除;\n");
printf("\t3修改:允许对已经录入的数据重新进行编辑、修改;\n");
printf("\t4抽奖:开始抽奖!\n");
printf("\t5显示:显示所有中奖者的信息;\n");
printf("\t6退出。\n");
printf("==>");
int operate;
fflush(stdin);
scanf("%d",&operate);
switch(operate)
{
case 1:
info_login();
break;
case 2:
info_delete();
break;
case 3:
info_modify();
break;
case 4:
award();
break;
case 5:
info_show();
break;
case 6:
return false;
default:
break;
}
printf("\n *** 作完成。\n");
return true;
}
void main()
{
while(raise());
}
帮你做了一个,不知是否满意呢?
import javaappletApplet;
import javaawtButton;
import javaawtColor;
import javaawtGraphics;
import javaawtTextField;
import javaawteventActionEvent;
import javautilRandom;
import javautilVector;
public class Lottery extends Applet {
private static final long serialVersionUID = 1L;
int w,h;
Button ok,out,setup;
String[] msg;
TextField[] gaiLv;
TextField[] jiangPin;
int mx,ml;
int maxNum;
Random ran;
Vector<Integer> fist;
Vector<Integer> sec;
Vector<Integer> third;
Vector<Integer> lucky;
boolean lot=false;
boolean iserr=false;
boolean issetup=false;
String mesg="输入错误";
String priseMsg="继续努力!";
public void init(){
w=400;
h=220;
mx=20;
ml=40;
ran=new Random();
thissetSize(w, h);
thissetLayout(null);
ok=new Button("抽奖");
out=new Button("退出");
setup=new Button("确认设置");
msg=new String[4];
msg[0]="一等奖";
msg[1]="二等奖";
msg[2]="三等奖";
msg[3]="幸运奖";
gaiLv=new TextField[4];
jiangPin=new TextField[4];
for(int i=0;i<4;i++){
gaiLv[i]=new TextField("00"+(i+1));
thisadd(gaiLv[i]);
gaiLv[i]setBounds(mx+ml, 75+i26, 60, 18);
jiangPin[i]=new TextField();
thisadd(jiangPin[i]);
jiangPin[i]setBounds(mx+ml3, 75+i26, 80, 18);
}
thisadd(ok);
oksetBounds(260, 180, 60, 28);
okaddActionListener(new LotButtonAction(this));
thisadd(out);
outsetBounds(330, 180, 60, 28);
outaddActionListener(new LotButtonAction(this));
thisadd(setup);
setupsetBounds(110, 180, 80, 24);
setupaddActionListener(new LotButtonAction(this));
}
public void paint(Graphics g){
gsetColor(Colorwhite);
gfillRect(0, 0, thisgetWidth(), thisgetHeight());
gsetColor(new Color(230,255,230));
gfillRect(0, 0, w, 30);
gsetColor(ColorBLUE);
gdrawString("JAVA抽奖系统", 130, 20);
gsetColor(ColorORANGE);
gdrawRect(10, 40, 230, 170);
gsetColor(ColorBLACK);
gdrawString("设置", mx, 60);
gdrawString("概率", mx+ml, 60);
gdrawString("奖品", mx+ml3, 60);
for(int i=0;i<msglength;i++){
gsetColor(new Color(255-(i30),45,89));
gdrawString(msg[i], 20, 90+i26);
}
if(lot==true){
gsetColor(new Color(rannextInt(255),rannextInt(255),rannextInt(255)));
if(priseMsglength()<=7){
gdrawString(priseMsg, 260, 100);
}else{
gdrawString(priseMsgsubstring(0, 5), 260, 100);
gdrawString(priseMsgsubstring(5), 260, 120);
}
}
if(iserr==true){
gdrawString(mesg, 260, 100);
}
}
public void getLucky(){
float firu=1;
float secu=1;
float thiu=1;
float fouu=1;
float minu=1;
if(gaiLv[0]getText()trim()length()>1){
firu=FloatparseFloat(gaiLv[0]getText());
if(firu<=0||firu>=1){
iserr=true;
return;
}
if(firu<minu){
minu=firu;
}
}
if(gaiLv[1]getText()trim()length()>1){
secu=FloatparseFloat(gaiLv[1]getText());
if(secu<=0||secu>=1){
iserr=true;
return;
}
if(secu<minu){
minu=secu;
}
}
if(gaiLv[2]getText()trim()length()>1){
thiu=FloatparseFloat(gaiLv[2]getText());
if(thiu<=0||thiu>=1){
iserr=true;
return;
}
if(thiu<minu){
minu=thiu;
}
}
if(gaiLv[3]getText()trim()length()>1){
fouu=FloatparseFloat(gaiLv[3]getText());
if(fouu<=0||fouu>=1){
iserr=true;
return;
}
if(fouu<minu){
minu=fouu;
}
}
if(minu>=1||minu<=0){
iserr=true;
return;
}
float aNum=1/minu;
maxNum=(int)aNum;
int count=(int)(firu/minu);
if(firu!=1){
fist=getLotteryVec(maxNum,count);
}else{
fistremoveAllElements();
}
count=(int)(secu/minu);
if(secu!=1){
sec=getLotteryVec(maxNum,count);
}else{
secremoveAllElements();
}
count=(int)(thiu/minu);
if(thiu!=1){
third=getLotteryVec(maxNum,count);
}else{
thirdremoveAllElements();
}
count=(int)(fouu/minu);
if(fouu!=1){
lucky=getLotteryVec(maxNum,count);
}else{
luckyremoveAllElements();
}
issetup=true;
iserr=false;
}
protected Vector<Integer> getLotteryVec(int maxNum,int num){
Vector<Integer> result=new Vector<Integer>();
for(int i=0;i<num;i++){
resultadd(rannextInt(maxNum));
}
return result;
}
protected int getaNum(){
return rannextInt(maxNum);
}
public int isLucky(int pNum){
for(int i=0;i<fistsize();i++){
if(fistget(i)==pNum){
return 1;
}
}
for(int i=0;i<secsize();i++){
if(secget(i)==pNum){
return 2;
}
}
for(int i=0;i<thirdsize();i++){
if(thirdget(i)==pNum){
return 3;
}
}
for(int i=0;i<luckysize();i++){
if(luckyget(i)==pNum){
return 4;
}
}
return -1;
}
public void ButtonActionPerformed(ActionEvent e){
String acName=egetActionCommand();
if(acNameequals("抽奖")){
if(issetup==false){
priseMsg="请先设置参数!";
lot=true;
repaint();
lot=false;
return;
}
lot=true;
priseMsg=getResult(getaNum());
repaint();
// lot=false;
}else if(acNameequals("退出")){
thissetVisible(false);
thisstop();
thisdestroy();
Systemexit(0);
}else if(acNameequals("确认设置")){
lot=false;
getLucky();
repaint();
}
}
public String getResult(int num){
int resu=isLucky(num);
String result="";
switch(resu){
case -1:
result="继续努力!";
break;
case 1:
result="恭喜你!一等奖!\n获得"+jiangPin[0]getText();
break;
case 2:
result="恭喜你!二等奖!\n获得"+jiangPin[1]getText();
break;
case 3:
result="恭喜你!三等奖!\n获得"+jiangPin[2]getText();
break;
case 4:
result="恭喜你!幸运奖!\n获得"+jiangPin[3]getText();
break;
default:
result="继续努力!";
}
return result+num;
}
}
class LotButtonAction implements javaawteventActionListener{
Lottery su;
public LotButtonAction(Lottery bun){
thissu=bun;
}
@Override
public void actionPerformed(ActionEvent e) {
suButtonActionPerformed(e);
}
}
以上就是关于怎么用python写一个抽奖程序,是抽取图片或视频全部的内容,包括:怎么用python写一个抽奖程序,是抽取图片或视频、用delphi7怎么做一个抽奖程序、c语言程序设计——抽奖程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)