本文为大家分享了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)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持软件开发网。