购物博弈系统
最近老师让我用python做一个银行系统,在研究了别人写的以后加上自己最近对博弈论有点兴趣。于是就有了今天这个捞比程序
程序结果是得到一系列功能,实现简单的商品出售、获利、存储等功能,有兴趣可以在上面多加一些功能。
首先,我们需要对我们的程序功能要有一些基础的绘制。
作为一个有账号资金的流通,肯定要先对每个人的账号进行定义,这里就有第一个买家类:
class Buyer(object): # 买家类
def __init__(self):
self.name = ''
self.idNum = ''
self.phoneNum = ''
self.cardNum = ''
self.cardPass_word = ''
self.goodsNum = 0 #需要(摆摊)出售商品数量
self.money = 0.0
self.goodspage = []
self.backpack = []
在进行游戏前,我们肯定需要每个人先注册账号,类里面的属性就是每个人该有的属性。’
这里我定义了9个, 开户名、身份证、电话号码、随机生成的卡号、密码、出售的商品种类数、账号资金、出售的商品名、已购买的商品。
然后因为每个商品有商品名以及对应的价格,这里我们再定义一个商品类:
class Goods(object): # 物品类
def __init__(self):
self.shop_object = ''
self.shop_price = 0.0
最后再定义一个商店类,里面包含了该程序需要的函数。
首先,为了便于客户用数字来选择商品,我们需要对添加的商品标号,还有要储存每个人的账号名以及商品序号对应的商品属性,这里用到两个字典来存储数据。
class Store(object):
buyers_list = [] # 储存买家卡号信息
buyers_dict = {} # 储存买家买卖信息,用卡号查询
goods_dict = {} # 输入要卖的商品信息
goods_number = 1
还有一个列表存储买家卡号信息,防止出现卡号重复的情况。
下面是主程序:
#__author:'lwq'
#date: 2018/12/20
import random,time,sys
class Goods(object): # 物品类
def __init__(self):
self.shop_object = ''
self.shop_price = 0.0
class Buyer(object): # 买家类
def __init__(self):
self.name = ''
self.idNum = ''
self.phoneNum = ''
self.cardNum = ''
self.cardPass_word = ''
self.goodsNum = 0 #需要(摆摊)出售商品数量
self.money = 0.0
self.goodspage = []
self.backpack = []
class Store(object):
buyers_list = [] # 储存买家卡号信息
buyers_dict = {} # 储存买家买卖信息,用卡号查询
goods_dict = {} # 输入要卖的商品信息
goods_number = 1
def card_num(self):#卡号
card = ''
while True:
for i in range(6):
strtotal = '123456789'
str1 = random.choice(strtotal)
card += str1
if card not in Store.buyers_list:
Store.buyers_list.append(card)
break
return card
def registered(self):#注册
buyer = Buyer()
buyer.name = input('>>姓名:')
buyer.idNum = input('>>身份证号:')
while True:
buyer.phoneNum = input('>>电话号码:')
if len(buyer.phoneNum) == 11:
break
else:
print('>>输入失败,请输入正确--')
while True:
buyer.cardPass_word = input('>>请输入登陆密码:')
re_cardpass = input('>>请确认输入密码:')
if buyer.cardPass_word == re_cardpass:
break
else:
print('>>两次输入不一致,重新输入--')
buyer.money = float(input('>>请输入储存金额:'))
card = Store.card_num(self)
buyer.cardNum = card
Store.buyers_dict[card] = buyer
print('>>开户成功,请记住卡号%s'%card)
def add_goods(self):#出售商品
#goods = Goods()
while True:
card = input('>>请输入您的卡号:')
if card not in Store.buyers_list:
print('>>没有找到相应信息,请重新输入--')
else:break
while True:
password = input('>>输入密码:')
if password == Store.buyers_dict[card].cardPass_word:
break
else:print('>>密码错误,请重新输入--')
while True:
goods = Goods()
goods.shop_object = input('>>要出售的商品:')
goods.shop_price = float(input('>>出售价格:'))
Store.goods_dict[Store.goods_number] = goods
Store.buyers_dict[card].goodspage.append(Store.goods_number)
Store.buyers_dict[card].goodsNum += 1
Store.goods_number += 1
whether = input('>>任意键继续--q退出')
if whether is 'q':
break
def count_detail(self): #查询功能
while True:
card = input('>>输入查询卡号:')
if card not in Store.buyers_list:
print('>>没有找到相应信息,请重新输入--')
else:break
while True:
password = input('>>输入密码:')
if password == Store.buyers_dict[card].cardPass_word:
print('>>账号持有人:', Store.buyers_dict[card].name)
print('>>绑定手机号码:', Store.buyers_dict[card].phoneNum)
print('>>在售商品数量:', Store.buyers_dict[card].goodsNum)
print('>>剩余金额:',Store.buyers_dict[card].money)
print('>>已购买商品:', ','.join(Store.buyers_dict[card].backpack))
break
else:
print('>>密码错误--重新输入-')
def save_money(self):#存钱
while True:
card = input('>>请输入您的卡号:')
if card not in Store.buyers_list:
print('>>没有找到相应信息,请重新输入--')
else:break
while True:
password = input('>>输入密码:')
if password == Store.buyers_dict[card].cardPass_word:
money = float(input('>>想要存储的金额:'))
Store.buyers_dict[card].money = Store.buyers_dict[card].money + money
print('>>存储成功--\n>>卡上余额:%.2f'%Store.buyers_dict[card].money)
break
def exit_total(self): #退出
print('>>正在退出--')
time.sleep(1)
sys.exit()
def change_pass(self):#修改密码
while True:
card = input('>>请输入您的卡号:')
if card not in Store.buyers_list:
print('>>没有找到相应信息,请重新输入--')
else:break
while True:
password = input('>>请输入您的旧密码:')
if password == Store.buyers_dict[card].cardPass_word:
#passnew = input('>>请输入您的新密码:')
while True:
Store.buyers_dict[card].cardPass_word = input('>>请输入您的新密码:')
re_cardpass = input('>>请确认输入密码:')
if buyer.cardPass_word == re_cardpass:
break
else:
print('>>两次输入不一致,重新输入--')
break
else:
print('>>输入密码错误请重新输入--')
def shoplist(self):#展示商品列表
print('>>商品列表-----------------------------')
for key in Store.goods_dict:
print('>>',key,'\t',Store.goods_dict[key].shop_object,'\t',Store.goods_dict[key].shop_price)
def shop_lose(self):#商品出售过程
while True:
card = input('>>请输入您的卡号:')
if card not in Store.buyers_list:
print('>>没有找到相应信息,请重新输入--')
else:break
while True:
password = input('>>输入密码:')
if password == Store.buyers_dict[card].cardPass_word:
break
while True:
number = int(input('>>输入你想要的商品序号:'))
if number in Store.goods_dict.keys():
if Store.buyers_dict[card].money < Store.goods_dict[number].shop_price :
print('>>购买失败--')
continue
for key in Store.buyers_dict.keys():
if number in Store.buyers_dict[key].goodspage:
Store.buyers_dict[key].money = Store.buyers_dict[key].money + Store.goods_dict[number].shop_price
Store.buyers_dict[card].money = Store.buyers_dict[card].money - Store.goods_dict[number].shop_price
Store.buyers_dict[card].backpack.append(Store.goods_dict[number].shop_object)
print('>>存储成功--\n卡上余额:%.2f' % Store.buyers_dict[card].money)
print('>>购买成功--')
print('>>已购买商品--%s'%','.join(Store.buyers_dict[card].backpack))
whether = input('>>任意键继续购买--q退出:')
if whether is 'q':
break
else:
None
def tkint(self):#普通界面
print("---------------------------------------------------------------------")
print("---------------------------------------------------------------------")
print("--* * *--")
print("--* * *--")
print("--* 购物博弈系统 * *--")
print("--* * *--")
print("--* * *--")
print("--* 快和小伙伴一起来玩吧 * *--")
print("---------------------------------------------------------------------")
print("---------------------------------------------------------------------")
time.sleep(1.5)
print('\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n')
while 1:
print("----------------------------------------------------------------------")
print("----------------------------------------------------------------------")
print("-- >>功能介绍: **--")
print("--** >>1.注册账号 >>2.创建商品 >>3.更改密码 **--")
print("--** >>4.购买商品 >>5.账户存储 >>6.查看商品 **--")
print("--------->>7.账户查询--------- ---------->>8.退出----------------------")
print("----------------------------------------------------------------------")
print("----------------------------------------------------------------------")
# try:
task = input('>>选择功能:')
if task == '1' :
self.registered()
elif task == '2':
self.add_goods()
elif task == '3':
self.change_pass()
elif task == '4':
self.shop_lose()
elif task == '5':
self.save_money()
elif task == '6' :
self.shoplist()
elif task == '7':
self.count_detail()
elif task == '8' :
self.exit_total()
else:
print('>>非法输入--')
# except:
# print('>>非法输入--')
buyer = Store()
buyer.tkint()
函数各功能我在后面都注释了,有不懂的随时来玩。
因为之前想做的是银行开户系统,但是半路想做一个这样的博弈内容。
游戏大概就是参与游戏的人赋予同样的资金,然后每个人上架同样的货物,然后每个人买几件物品,最后看谁买到的商品总价值最高。
对了,游戏中商品可重复购买。
游戏博弈在于,你出售的商品定价是否受更多人的选择。