本文為大家分享了python實現彩票系統的具體代碼,供大家參考,具體內容如下
功能:1、注冊 2、登錄 3、充錢 4、提現 5、下注 6、開獎 7、退出
簡述:彩民需要用身份證號碼開戶注冊一個彩票號碼,購買彩票時需要先登錄,可以充值以及提現。購買的彩票金額可以自己給定。此系統主要采用面向對象的方法,信息存儲方式采用pickle模塊來進行存儲。
系統主函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
from lotterysystem import lotterySystem import displayview import os import pickle import time def main(): displayview.display() path = r "H:\myPythonFile\彩票系統\彩票系統\userInformation.txt" f = open (path, "rb" ) # uersdict = {} uersdict = pickle.load(f) # 打印出所有用戶的信息 for x in uersdict: print ( "name:%s idCard:%s phone:%s cardNum:%s password:%s money:%d" % (uersdict.get(x).name, uersdict.get(x).idCard, uersdict.get(x).phoneNum, uersdict.get(x).card.cardNum, uersdict.get(x).card.password, uersdict.get(x).card.cardMoney)) f.close() lotterysystem = lotterySystem(uersdict) islogin = None while True : time.sleep( 3 ) displayview.chooseview() operation = input ( "請選擇您的操作:" ) if operation = = '1' : islogin = lotterysystem.register() elif operation = = '2' : islogin = lotterysystem.login() print ( "登錄成功!" ) elif operation = = '3' : if islogin: lotterysystem.charge(islogin) else : print ( "請先登錄..." ) elif operation = = '4' : if islogin: lotterysystem.embody(islogin) else : print ( "請先登錄..." ) elif operation = = '5' : if islogin: buyLotteryNum, buymoney = lotterysystem.buylottery(islogin) else : print ( "請先登錄..." ) elif operation = = '6' : if islogin: lotterysystem.kaijiang(islogin, buymoney, buyLotteryNum) else : print ( "請先登錄..." ) elif operation = = '7' : if islogin: lotterysystem.quit(islogin) print ( "退出成功!" ) f = open (path, "wb" ) pickle.dump(uersdict, f) f.close() return - 1 else : print ( "請先登錄..." ) else : print ( "操作有誤!系統自動退出..." ) return - 1 if __name__ = = "__main__" : main() |
用pickle模塊讀取文件信息時,文件必須有內容,否則會報錯。所以,第一次執行該程序時,必須先將信息寫入文件后才能讀。
系統界面函數:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
def display(): print ( """ ******************************************* * * * * * 歡迎來到木子李彩票系統 * * * * * ******************************************* """ ) def chooseview(): print ( """ ******************************************* * 1、注冊 2、登錄 * * 3、充錢 4、提現 * * 5、下注 6、開獎 * * * * 7、退出 * ******************************************* """ ) |
系統功能函數:實現注冊等功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
from uers import Uers from card import Card import check import random class lotterySystem(): def __init__( self , userDict): self .userDict = userDict def register( self ): name = input ( "請輸入您的名字:" ).strip() if not check.checkName(name): return - 1 idCard = input ( "請輸入您的身份證號碼:" ).strip() if not check.checkIdentity(idCard): return - 1 phoneNum = input ( "請輸入您的電話號碼:" ).strip() if not check.checkPhone(phoneNum): return - 1 cardNum = check.getlotteryCardNum() while True : if self .userDict.get(cardNum) ! = None : card = check.getBankCardNum() else : break prestoredMoney = int ( input ( "請輸入預存款金額:" )) if not check.checkPrestoredMoney(prestoredMoney): return - 1 onePassword = input ( "請設置您的密碼:" ).strip() for x in range ( 2 ): if not check.checkPassword(onePassword): print ( "密碼輸入不正確!" ) else : break else : print ( "密碼輸入不正確!開戶失敗..." ) return - 1 card = Card(cardNum, onePassword, prestoredMoney) uers = Uers(name, idCard, phoneNum, card) self .userDict[cardNum] = uers print ( "恭喜您!注冊成功,請牢記您的卡號:" , cardNum) return cardNum def login( self ): cardNum = input ( "請輸入您的卡號:" ).strip() password = input ( "請輸入您的密碼:" ).strip() uers = self .userDict.get(cardNum) if uers = = None or uers.card.password ! = password: print ( "該卡不存在或密碼不正確!登錄失敗..." ) return False return cardNum def charge( self , cardNum): money = int ( input ( "請輸入充值金額:" )) if money > 0 : pass else : print ( "充值失敗..." ) return - 1 self .userDict.get(cardNum).card.cardMoney + = money print ( "充值成功,余額 %d 元:" % self .userDict.get(cardNum).card.cardMoney) def embody( self , cardNum): print ( "余額: %d 元" % self .userDict.get(cardNum).card.cardMoney) money = int ( input ( "請輸入提現金額:" )) if money < self .userDict.get(cardNum).card.cardMoney: pass else : print ( "余額不足!提現失敗..." ) return - 1 self .userDict.get(cardNum).card.cardMoney - = money print ( "提現成功,余額: %d 元" % self .userDict.get(cardNum).card.cardMoney) def buylottery( self , cardNum): lotteryNum = input ( '請輸入所購買的彩票的序列號(6位數字,只含0和1):' ) while True : for x in lotteryNum: if len (lotteryNum) = = 6 : pass else : lotteryNum = input ( '輸入位數有誤!請重新輸入...:' ) if x = = '0' or x = = '1' : pass else : lotteryNum = input ( '輸入數字有誤!請重新輸入...:' ) break buymoney = int ( input ( "請輸入購買金額:" )) if buymoney < self .userDict.get(cardNum).card.cardMoney: pass else : print ( "余額不足!購買失敗..." ) return - 1 self .userDict.get(cardNum).card.cardMoney - = buymoney print ( "購買成功!購買號碼為:" ,lotteryNum) print ( '購買彩票需要 %d 元!您的余額還有 %d 元' % (buymoney, self .userDict.get(cardNum).card.cardMoney)) return lotteryNum, buymoney def kaijiang( self , cardNum, buymoney, lotteryNum): lotteryStr = '' for x in range ( 6 ): lotteryStr + = random.choice([ '0' , '1' ]) print ( '開獎號碼為:' , lotteryStr) lotteryNum = input ( "werwe" ) if lotteryNum = = lotteryStr: self .userDict.get(cardNum).card.cardMoney + = buymoney * 200 print ( "恭喜您中了 %d 元大獎!!!您的彩票卡余額還有 %d 元" % (buymoney * 200 , self .userDict.get(cardNum).card.cardMoney)) else : print ( "很遺憾沒中獎,繼續努力!" ) def quit( self , cardNum): print ( '您的余額還有 %d 元' % self .userDict.get(cardNum).card.cardMoney) |
系統驗證函數:主要用于驗證身份證、手機號、卡號等是否正確
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import random def checkName(user): if len (user) ! = 1 : print ( "用戶名長度不合法!" ) return False for x in user: if x > = '0' and x < = '9' or x > = 'a' and x < = 'z' or x > = 'A' and x < = 'Z' or x = = '_' : pass else : print ( "用戶名輸入不合法!" ) return False return True # 檢查身份證號碼是否合法 def checkIdentity(identity): if len (identity) ! = 1 : print ( "身份證長度不合法!" ) return False for x in identity: if x > = '0' and x < = '9' : pass else : print ( "身份證輸入不合法!" ) return False return True # 檢查電話號碼是否合法 def checkPhone(identity): if len (identity) ! = 1 : print ( "電話號碼長度不合法!" ) return False for x in identity: if x > = '0' and x < = '9' : pass else : print ( "電話號碼輸入不合法!" ) return False return True # 檢查預存款金額是否合法 def checkPrestoredMoney(money): if money > 0 : return True else : print ( "預存款金額輸入有誤!" ) return False # 檢查密碼是否正確 def checkPassword(password): AgainPassword = input ( "請確認您的密碼:" ).strip() if password = = AgainPassword: return True else : return False # 產生一個卡號 def getlotteryCardNum(): bankCardNum = "" for i in range ( 6 ): bankCardNum + = chr (random.randrange( 10 ) + 48 ) return bankCardNum if __name__ = = "__main__" : pass |
用戶信息:主要用于存儲用戶的信息。
1
2
3
4
5
6
7
8
9
|
class Uers(): def __init__( self , name, idCard, phoneNum, card): self .name = name self .idCard = idCard self .phoneNum = phoneNum self .card = card def __str__( self ): return "%s-%s-%s-%s" % ( self .name, self .idCard, self .phoneNum, self .card) |
卡信息:主要用于存儲卡的信息
1
2
3
4
5
6
7
8
|
class Card(): def __init__( self , cardNum, password, cardMoney): self .cardNum = cardNum self .password = password self .cardMoney = cardMoney def __str__( self ): return "%s-%s-%d" % ( self .cardNum, self .password, self .cardMoney) |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/YiHong_Li/article/details/81407158