python实现简单的购物车

Posted Mr-Lf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python实现简单的购物车相关的知识,希望对你有一定的参考价值。

import json,time
userinfo={"lanfei":
{ "passwd":"lanfei",
"salary":3000,
"buy_commodity":{ "time":None,
"commodity_name":None,
"price":None,},
"shopping_cart":{
"commodity_name":None,
"price":None,}
}
}
commodity_list={"ipad":{"price":100,"orign_place":"北京"}}
def import_to_file():
try:
f1=open(r"F:userinfo.txt",‘w‘,encoding="utf-8")
f2=open(r"F:commodity.txt",‘w‘,encoding="utf-8")
json.dump(userinfo,f1)
json.dump(commodity_list,f2)
f1.close()
f2.close()
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
def export_from_file():
global userinfo
global commodity_list
global commodity_list
try:
f1=open(r"F:userinfo.txt",‘r‘,encoding="utf-8")
f2=open(r"F:commodity.txt","r",encoding="utf-8")
userinfo=json.load(f1)
commodity_list=json.load(f2)
f1.close()
f2.close
except FileNotFoundError as e:
print(e)
except Exception as e:
print(e)
def creat_user(name,passwd):
export_from_file()
if name in userinfo.keys():
print("usernam has exist!")
name=input("rewrite your username:")
passswd=input("rewrite your userpassswd:")
creat_user(name,passwd)
else:
userinfo.setdefault(name,{"passwd":passwd,"salary":None})
return True
def add_commmdity(mount):
print("商品添加".center(30))
print(‘-‘*35)
for i in range(mount):
commodity=input("输入要添加的商品:")
price=eval(input("输入商品的价格:"))
info=input("输入商品的信息:")
commodity_list.setdefault(commodity,{"price":price,"orign_place":None})
if commodity not in commodity_list.keys():
print("%s添加失败"%commodity)
else:
print("全部添加成功")
import_to_file()

def authentication(name,passwd):
export_from_file()
if name in userinfo.keys():
if passwd==userinfo[name]["passwd"]:
return True
else:
return False
else:
return False
def shopping(name):
while True:
print("商品列表".center(30))
print("-" * 35)
for index,item in commodity_list.items():
print("%s: %s %s"%(index,item["price"],item["orign_place"]))
commodity_name=input("输入您想要购买的商品名称:")
while commodity_name not in commodity_list.keys():
commodity_name=input("商品不存在,请重新输入:")
price=commodity_list[commodity_name]["price"]
if userinfo[name]["salary"]>=price:
ack=input("确认购买(y/n):")
if ack=="y":
userinfo[name]["salary"]-=price
userinfo[name].setdefault("buy_commodity",{"time":time.ctime(),"price":price,"commdity_name":commodity_name})
print("购买成功,您的余额:%s"%userinfo[name]["salary"])
elif ack==‘no‘:
continue
elif ack==‘exit‘:
break
else:
print("余额不足,请充值!")
ack=input("是否退出程序(y/n):")
if ack=="y":
exit()
else:
continue
def user_login_interface():
function=input("登录输入login,注册输入regist:")
if function !="login" and function != "regist":
print("输入错误!")
name=input("username:")
passwd=input("userpassswd:")
if function=="login":
if authentication(name,passwd)==True:
print("login successfully!")
shopping(name)
else:
print("username or userpasswd error!")
elif function=="regist":
if creat_user(name,passwd)==True:
import_to_file()
print("regist sucessfully!")
def merchant_login_interface():
function=input("登录输入login,注册输入regist:")
if function !="login" and function != "regist":
print("输入错误!")
exit()
name=input("username:")
passwd=input("userpassswd:")
if function=="login":
if authentication(name,passwd)==True:
print("login successfully!")
mount=eval(input("输入添加商品的数目:"))
add_commmdity(mount)
else:
print("username or userpasswd error!")
elif function=="regist":
if creat_user(name,passwd)==True:
import_to_file()
print("regist sucessfully!")
#user_login_interface()
merchant_login_interface()










































































































































以上是关于python实现简单的购物车的主要内容,如果未能解决你的问题,请参考以下文章

python实现简单的循环购物车小功能

python 实现简单购物车

python实现简单的购物车

python 字典实现简单购物车

python实现简单的购物程序

python 用列表实现简单购物功能