您好,登錄后才能下訂單哦!
小編給大家分享一下python實現彩票系統的方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
功能:1、注冊 2、登錄 3、充錢 4、提現 5、下注 6、開獎 7、退出
簡述:彩民需要用身份證號碼開戶注冊一個彩票號碼,購買彩票時需要先登錄,可以充值以及提現。購買的彩票金額可以自己給定。此系統主要采用面向對象的方法,信息存儲方式采用pickle模塊來進行存儲。
系統主函數:
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模塊讀取文件信息時,文件必須有內容,否則會報錯。所以,第一次執行該程序時,必須先將信息寫入文件后才能讀。
系統界面函數:
def display(): print(""" ******************************************* * * * * * 歡迎來到木子李彩票系統 * * * * * ******************************************* """) def chooseview(): print(""" ******************************************* * 1、注冊 2、登錄 * * 3、充錢 4、提現 * * 5、下注 6、開獎 * * * * 7、退出 * ******************************************* """)
系統功能函數:實現注冊等功能
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)
系統驗證函數:主要用于驗證身份證、手機號、卡號等是否正確
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
用戶信息:主要用于存儲用戶的信息。
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)
卡信息:主要用于存儲卡的信息
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)
看完了這篇文章,相信你對“python實現彩票系統的方法”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。