1. 程式人生 > >python實例_購物車

python實例_購物車

pan == for spa get exit else 高亮 sdi

一,作者介紹

吳海東:My Blog:http://www.cnblogs.com/whd-672701

二,程序說明

購物車程序:

1、啟動程序後,輸入用戶名密碼後,如果是第一次登錄,讓用戶輸入工資,然後打印商品列表

2、允許用戶根據商品編號購買商品

3、用戶選擇商品後,檢測余額是否夠,夠就直接扣款,不夠就提醒

4、可隨時退出,退出時,打印已購買商品和余額

5、在用戶使用過程中, 關鍵輸出,如余額,商品已加入購物車等消息,需高亮顯示

6、用戶下一次登錄後,輸入用戶名密碼,直接回到上次的狀態,即上次消費的余額什麽的還是那些,再次登錄可繼續購買

7、允許查詢之前的消費記錄

三,程序前準備

創建一個購物物品文件“shopping”,物品可自行定義。

技術分享

創建一個存放已購買物品的文件“lock_file”,開始默認為空。

四,程序腳本



shangpin = open("shopping",r,encoding="utf-8")
shangpin = list(shangpin)
shopping_list = []

def test():
    for index, i in enumerate(shangpin):
        print(index, i)
    mai = input("Want to buy what?>>>:")
    return mai


def test_1():
        jin 
= input("Please enter the amount:") if jin.isdigit(): jin = int(jin) loginSucces = False while True: mai = test() if mai.isdigit(): mai = int(mai) if mai < len(shangpin) and mai >= 0: kemai
= shangpin[mai] kemai = kemai.strip().split(,) kemaii = int(kemai[1]) if kemaii <= jin: shopping_list.append(kemai) jin -= kemaii print("\033[32;1mBuy the goods:%s,The balance remaining:%s\033[0m" % (kemai, jin)) loginSucces = False while True: jixu = input("繼續購買按y,或是退出按q:") if jixu.isalpha(): jixu = str(jixu) if jixu == y: break if loginSucces == True:continue elif jixu == q: print("\033[31;1m-----You bought the following goods altogether----") for p in shopping_list: print(p) f = open("lock_file", a+, encoding="utf-8") f.write("{\"%s\":{\"name\":\"%s\",\"shopping\":%s,\"remaining\":%s}}\n" % (str(username), str(username), str(shopping_list), str(jin))) f.close() if loginSucces == True: break print("The balance remaining:%s\033[0m" % jin) exit() if loginSucces == True: break continue else: print("Insufficient balance to buy:%s" % jin) loginSucces = False while True: jixu = input("繼續購買按y,或是退出按qq:") if jixu.isalpha(): jixu = str(jixu) if jixu == y: break elif jixu == q: print("\033[31;1m-----You bought the following goods altogether----") for p in shopping_list: print(p) f = open("lock_file", a+, encoding="utf-8") f.write("{\"%s\":{\"name\":\"%s\",\"shopping\":%s,\"remaining\":%s}}\n" % ( str(username), str(username), str(shopping_list), str(jin))) f.close() print("The balance remaining:%s\033[0m" % jin) exit() if loginSucces == True:break def test_2(): for date in dataa: if username in date: print(dataa[username]) else: continue jin = dataa[username]["remaining"] jine = jin jin = str(jin) if jin.isdigit(): jin = int(jin) loginSucces = False while True: mai = test() if mai.isdigit(): mai = int(mai) if mai < len(shangpin) and mai >= 0: kemai = shangpin[mai] kemai = kemai.strip().split(,) kemaii = int(kemai[1]) if kemaii <= jin: aa = dataa[username]["shangpin"] aaa = aa aaa = str(aaa) shopping_list = dataa[username]["shangpin"] shopping_list.append(kemai) print(shopping_list) shopping_li = shopping_list shopping_li = str(shopping_li) jin -= kemaii jin = str(jin) jine = str(jine) print("\033[32;1mBuy the goods:%s,The balance remaining:%s\033[0m" % (kemai, jin)) loginSucces = False while True: jixu = input("繼續購買按y,或是退出按q:") if jixu.isalpha(): jixu = str(jixu) if jixu == y: break if loginSucces == True: continue elif jixu == q: print("\033[31;1m-----You bought the following goods altogether----") for p in shopping_list: print(p) with open("lock_file","r",encoding="utf-8") as f: lines = f.readlines() with open("lock_file","w",encoding="utf-8") as f_w: for line in lines: if username in line: line = line.replace(aaa,shopping_li) f_w.write(line) f_w.close() with open("lock_file", "r", encoding="utf-8") as f: lines = f.readlines() with open("lock_file", "w", encoding="utf-8") as f_q: for line in lines: if username in line: line = line.replace(jine,jin) f_q.write(line) f_q.close() print("The balance remaining:%s\033[0m" % jin) exit() if loginSucces == True: break username = input(xinuser:).strip() with open("lock_file",r,encoding="utf-8") as f: for data in f: if len(data) != 0: dataa = eval(data) for date in dataa: daa = date if username in daa: break elif username not in daa: continue test_2() else: password = input(passwd:).strip() test_1()
 

 

python實例_購物車