難點: 屢次購買出現bug,之後再優化吧python
購物車程序:
一、啓動程序後,輸入用戶名密碼後,若是是第一次登陸,讓用戶輸入工資,而後打印商品列表
二、容許用戶根據商品編號購買商品
三、用戶選擇商品後,檢測餘額是否夠,夠就直接扣款,不夠就提醒
四、可隨時退出,退出時,打印已購買商品和餘額
五、在用戶使用過程當中, 關鍵輸出,如餘額,商品已加入購物車等消息,需高亮顯示
六、用戶下一次登陸後,輸入用戶名密碼,直接回到上次的狀態,即上次消費的餘額什麼的仍是那些,再次登陸可繼續購買
七、容許查詢以前的消費記錄git
shopping.py 運行文件【同目錄再建立個data文件夾】app
#!/use/bin/env python # -*- coding:utf-8 -*- import os BASE_DIR = os.path.dirname(__file__) # 當前路徑 DATA_DIR = os.path.join(BASE_DIR,'data') # 指定路徑 def login(): # 登陸 username = input("請輸入用戶名:") if user_exit(username): # 檢測用戶是否存在 while True: password = input("請輸入密碼:") new_user_file = os.path.join(DATA_DIR, username) with open(new_user_file,"r",encoding="utf-8") as f: f1 = f.read() if f1.split()[1] == password: # 判斷密碼是否正確 lishi = input("是否查詢消費記錄,請輸入y/n:") if lishi == 'y' or lishi == 'Y': with open(new_user_file, "r",encoding="utf-8") as f: for index, i in enumerate(range(3)): s = f.readline().strip() if index == 2: if s[14:-1] =="": # 判斷記錄爲空的話提示暫無購物記錄 print("\033[31;1m暫無購物記錄\033[0m") print(s[14:-1]) # print("-----",f.split()[5]) money = int(f1.split()[3]) shopping(username,money,password) break else: print("密碼錯誤,請從新輸入密碼!") else: # 不存在的話就跳到註冊 register(username) return True def user_exit(name): # 檢測用戶是否存在 user_info_file = os.path.join(DATA_DIR,name) if os.path.exists(user_info_file): # print("此用戶文件存在!") return True else: print("此用戶文件不存在,歡迎註冊。") return False def register(name): # 新用戶註冊 print("您是新用戶",name) while True: password = input("請輸入新密碼-->:") if len(password.strip()) > 0: new_user(name,password) print("註冊成功!") return login() else: print("密碼不能爲空,或空格,請從新輸入-->:") def new_user(name,password): # 把新用戶的信息轉成str保存到文本中 user_money = input("您是新用戶請輸入工資:") user_list = '''password %s'''.strip()%password\ +"\n"+'''balance %s'''.strip()%user_money\ +"\n"+'''shopping_log []'''.strip() new_user_file = os.path.join(DATA_DIR,name) with open(new_user_file,"w",encoding="utf-8") as f: f.write(str(user_list)) def shopping(username,money,password): # 購買商品 shopping = [] goods = [("IPhone8", 6000), ("book", 20), ("Python", 80), ("JAVA", 65), ("Mac", 7000)] while True: for index, goods_list in enumerate(goods): print(index, goods_list) user_choice = input("請選擇您要購買的商品編號,按q退出:") if user_choice.isdigit(): # 判斷是否數字類型 user_choice = int(user_choice) if user_choice < len(goods): # 判斷長度是否超過了goods長度 goods_list = goods[user_choice] if goods_list[1] < money: # 買的起 shopping.append(goods_list) money -= goods_list[1] print("\033[31;1m您已購買:%s,餘額爲:%s\033[0m" % (goods_list, money)) else: # 買不起 print("餘額不足!只剩下%s了" % (money)) else: print("請輸入正確的商品編號:") elif user_choice == 'q': print("--------購物清單---------") new_user_file = os.path.join(DATA_DIR, username) with open(new_user_file, "r",encoding="utf-8") as f: for index, i in enumerate(range(3)): s = f.readline().strip() if index == 2: s=s[14:-1] #獲取歷史購物信息 if s =="": # 判斷歷史記錄爲空的話添加,有歷史記錄的話追加 user_shopping = [] else: user_shopping = [s] # 定義列表(歷史購物信息也在列表中) for sp in shopping: print(sp) user_shopping.append(sp) #購買商品追加到列表 # print("user_shopping-----",user_shopping) print("您的餘額爲%s" % (money)) user_list = '''password %s'''.strip() % password+ "\n" + ''' balance %s'''.strip() % money+ "\n" + ''' shopping_log %s'''.strip()%(user_shopping) new_user_file = os.path.join(DATA_DIR, username) with open(new_user_file, "w", encoding="utf-8") as f: f.write(str(user_list)) exit() else: print("請輸入正確的商品編號:") login()