欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现*

程序员文章站 2023-08-25 13:20:26
本文为大家分享了python实现*的具体代码,供大家参考,具体内容如下 功能:1、注册  2、登录  3、充钱   4、提现...

本文为大家分享了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)

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。