1. 程式人生 > >python實現簡單的購物車

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:\n\t%s\n\t%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()