设为首页 加入收藏

TOP

购物车--用户和商户入口(一)
2017-10-10 21:06:07 】 浏览:3788
Tags:购物车 用户 商户 入口
'''
此模块需要使用用户登陆模块代码

用户入口:
1、输入用户名和密码
2、商品信息存在文件里
3、已购商品和余额记录在文件里

注:用户购买商品后退出,再次登陆会自动读取上次用户的余额

商户入口:
1、添加商品
2、修改商品名称和价格
3、删除商品

备注:需要新建shop文件和购物车文件,其中shop文件保存商品信息:


'''
import os
import userLogin
import types

def read_info(_name):
if os.path.getsize("购物车"):
remaining = 0
with open("购物车","r",encoding="utf-8") as f:
for line in f:
if line.strip() == "":
continue
line = eva l(line)
if line["name"] == _name:
remaining = line["shopping"][2]
if remaining > line["shopping"][2]:
remaining = line["shopping"][2]
if remaining == 0:
return None
else:
return remaining
else:
return None


def memu(num):
if os.path.getsize("shop"):
with open("shop","r",encoding="utf-8") as f:
print("------------商品------------")
for index,line in enumerate(f):
if line.strip() == "":
continue
index -= 1
line = eva l(line)
info2 = '''
{_index}、{_goods},价格:{_price}
'''.format(_index = index + 1,
_goods = line["goods"],
_price = line["price"])
print(info2)
else:
print("当前无商品")
if num == 1:
exit()

def shopping_list(name):
sum = 0
flag = 0
with open("购物车", "r+", encoding="utf-8") as f:
for temp in f:
if temp.strip() == "":
continue
temp = eva l(temp)
if temp["name"] == name:
sum += temp["shopping"][1]
info2 = '''
商品:{_goods},价格:{_price}
'''.format(_goods=temp["shopping"][0],
_price=temp["shopping"][1])
if flag == 0:
print("---------购物清单---------")
print(info2)
flag = 1
print("总计金额: %d" % sum)


def user(_momey,name):
choose = input("输入你要购买商品的编号 >>>:")
if choose.isdigit():
choose = int(choose)
elif choose == 'q':
shopping_list(name)
exit()
else:
print("输入错误")
res = read_info(name)
if res != None:
_momey = res
f = open("shop", "r", encoding="utf-8")
content = f.readlines()
for i in range(len(content)):
if choose - 1 == i:
content[i] = eva l(content[i])
surplus = _momey - content[i]["price"]
if surplus >= 0:
_momey = surplus
print("\033[33;1m %s \033[0m 已加入购物车,你的当前余额为:\033[32;1m %d \033[0m "%(content[i]["goods"],_momey))
save(content[i],name,_momey)
return _momey
else:
print("\033[31;1m 你的余额不足,当前余额为:%d \033[0m"%_momey)
return _momey
f.close()
def save(d,name,momey):
dict = {}
dict["name"] = name
dict["shopping"] = [d["goods"],d["price"],momey]
with open("购物车","a+",encoding="utf-8") as f:
f.write(str(dict) + '\n')

def deposit(name):
while True:
res = read_info(name)
if res == None:
momey = input("输入你的存款金额 >>>:")
else:
momey = res
print("\033[34;1m %s你好,你的当前余额是:%d \033[0m"%(name,momey))
return momey
if type(momey) != type(1):
if momey.isdigit():
momey = int(momey)
return momey
elif momey == 'q':
exit()
else:
print("存款输入错误")
continue

def add_shop():
with open("shop","a+",encoding="utf-8") as f:
dict = {}
goods = input("输入你要添加的商品名称 >>>")
price = input("输入商品的价格(正整数) >>>")
if price.isdigit():
price = int(price)
dict["goods"] = goods
dict["price"] = price
f.wri
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python操作excel表格 下一篇python3精品解析运算符

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

最新文章

热门文章

C 语言

C++基础

windows编程基础

linux编程基础

C/C++面试题目