python制作抽獎程序代碼詳解
實現制作抽獎程序,需要認知到我們可以看到一般抽獎程序界面上是有很多按鈕的,比如中獎區域,按鍵開始區域等等,所以我們先要設置界面,然后把這些按鈕添加到界面中去,想必這對于學過tkinter的同學應該不難。下面結合實現步驟:設計界面、利用循環、多線程來完成抽獎程序設置吧。
實現代碼:
import random #導入內置的random模塊list1=list(range(0,15)) #將range元素進行列表轉換并賦值給列表list1print('抽獎號碼是:',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,取隨機數的模塊randomdata = 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中存儲的值
到此這篇關于python制作抽獎程序代碼詳解的文章就介紹到這了,更多相關如何使用python制作抽獎程序內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章: