Python實現購物車小程序
開發環境,win7、Python3.6、Pycharm社區版2017
作業需求:
購物車程序:
1、啟動程序後,輸入用戶名密碼後,如果是第一次登錄,讓用戶輸入工資,然後打印商品列表 #再次登陸,輸入工資進行充值服務,有沒有人性化
2、允許用戶根據商品編號購買商品
3、用戶選擇商品後,檢測余額是否夠,夠就直接扣款,不夠就提醒
4、可隨時退出,退出時,打印已購買商品和余額
5、在用戶使用過程中, 關鍵輸出,如余額,商品已加入購物車等消息,需高亮顯示
6、用戶下一次登錄後,輸入用戶名密碼,直接回到上次的狀態,即上次消費的余額什麽的還是那些,再次登錄可繼續購買
7、允許查詢之前的消費記錄 #不用查詢,下次登錄自動打印!苦笑
先寫個不調用文件的“購物車”模擬練習一下:
這是超市的商品列表:
1 mart_list = [ 2 ("Iphone",5500), 3 ("Mac Book",9800), 4 ("Coffee",29), 5 ("ThinkPad",6690), 6 ("vcd",100), 7 ("Channel",2222) 8 ]
這是購物車主程序:
1 shopping_car = [] 2 while True: 3 salary = input("input your salary: ") 4 if salary.isdigit():5 salary = int(salary) 6 print("當前余額:\033[31m%s\033[0m"%salary) 7 while True: 8 print("product list".center(50,"=")) 9 for k,i in enumerate(mart_list): 10 print(k,i) 11 choice = input("\033[36m購物愉快!請選擇商品:\033[0m") 12 ifchoice.isdigit(): 13 choice = int(choice) 14 if choice in range(len(mart_list)): 15 buy_product = mart_list[choice] 16 if salary < buy_product[-1]: 17 print(‘\033[45m余額不足\033[0m 當前余額僅剩:\033[31m%s\033[0m‘%salary) 18 else: 19 print(‘\033[43m%s\033[0m 已加入購物車‘%buy_product[0]) 20 shopping_car.append(buy_product) 21 salary -= buy_product[-1] 22 print(‘*‘*16,‘ 當前余額:\033[31m%s\033[0m‘ % salary) 23 else: 24 print(‘\033[42m持續更新中,盡情期待……\033[0m‘) 25 elif choice =="q": 26 print(‘你已購商品列表‘.center(42,‘-‘)) 27 for i,k in enumerate(shopping_car): 28 print(i,k) 29 print(‘本次消費:怎麽算,余額:\033[31m%s\033[0m‘%salary) 30 exit() 31 else: 32 print(‘\033[42m你的心思,我不太明白!\033[0m‘) 33 else: 34 print("\033[41m是不是輸入的是假幣呢?\033[0m") 35 continue
代碼的堆砌,終於完成,總體感覺還能看,
但是下面的整體:哎,接招吧!爛?我也感覺是
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Channel2 mart_list = [ ("Iphone",5500), ("Mac Book",9800), ("Coffee",29), ("ThinkPad",6690), ("VCD",100), ("Channel",2222) ] # shopping_car = [("ThinkPad",6690),("ThinkPad",6690),("vcd",100)] # with open(‘userinfo.txt‘, ‘r‘, encoding=‘utf-8‘) as f: # # for k in f: # # print(k) # if username in k: # print(k) # username = k.split(‘:‘)[0] # password = k.split(":")[1] # salary = k.split(‘:‘)[2] # last_shopping = k.split(":")[-1] # print(username,password,salary,last_sho # while True: while True: username = input(‘用戶名:‘) with open(‘lockuser.txt‘,‘r‘,encoding=‘utf-8‘) as f: for k in f: if username not in k: while True: with open(‘userinfo.txt‘,‘r+‘,encoding=‘utf-8‘) as f: for k in f: if username in k: username=k.split(";")[0] real_passwd = k.split(‘;‘)[1] salary = int(k.split(‘;‘)[-2]) last_shopping = k.split(‘;‘)[-1] # print(‘‘) count = 0 while True: password = input(‘請輸入密碼:‘) if password == real_passwd: print(‘welcome to my supermarket‘) if last_shopping == "0": pass else: print(‘上次購物是:%s‘%last_shopping) print(‘你當前余額為:%s‘%salary) # if salary == 0: while True: add_money = input(‘你的工資是:‘) if add_money.isdigit(): add_money = int(add_money) salary += add_money print(‘你當前總金額為:\033[31m%s\033[0m‘ % salary) break else: print(‘\033[41m無法識別你的金額!\033[0m 稍稍充值點唄!‘) # else: shopping_car = [] while True: print("product list".center(50, "=")) for k, i in enumerate(mart_list): print(k, i) choice = input("\033[33m購物愉快!請選擇商品:\033[0m") if choice.isdigit(): choice = int(choice) if choice in range(len(mart_list)): buy_product = mart_list[choice] if salary < buy_product[-1]: print(‘\033[43m余額不足\033[0m 當前余額僅剩:\033[31m%s\033[0m‘ % salary) else: print(‘\033[45m%s\033[0m 已加入購物車‘ % buy_product[0]) # with open(‘userinfo‘,‘r+‘,encoding=‘utf-8‘) as f: shopping_car.append(buy_product) salary -= buy_product[-1] print(‘當前余額:\033[31m%s\033[0m‘%salary) else: print(‘\033[46m正在更新中,盡情期待\033[0m‘) elif choice == "q": print(‘你已購商品列表‘.center(40,‘-‘)) for i, k in enumerate(shopping_car): print(i, k) print(‘本次消費後,余額\033[31m%s\033[0m‘ % salary) with open(‘userinfo.txt‘,‘r+‘,encoding=‘utf-8‘) as f: f.read() # print(f.tell()) f.write(‘;‘) f.write(str(salary)) # f.write(‘;‘,str(salary)) f.flush() f.write(‘;‘) f.write(str(shopping_car)) f.flush() exit() else: print(‘\033[32m你的心思,我很難猜!\033[0m‘) else: if count == 2: print("\033[42m賬號錯誤太多,將被鎖定,請聯系管理員!\033[0m") with open(‘lockuser.txt‘,‘w‘,encoding=‘utf-8‘) as f: f.write(username) exit() else: print(‘\033[43m密碼錯誤!\033[0m‘) count += 1 else: print(‘用戶不存在‘) exit() else: print(‘此賬號被鎖定,請手動刪除lock.user.txt的內容,重新再試!‘) exit()
好歹也辛苦構思三天了,要不我給你指導一下?
首先文件結構:主程序就不用說了,輔助兩個文件lockuser.txt當密碼輸入錯誤超過三次,則鎖定。
userinfo.txt保存用戶的用戶名;密碼;余額;上次購買物品。下次再購買,則繼續追加“;余額” 和 “;本次購買的商品”
1.運行主程序,輸入用戶名,和密碼
2.讀取賬號余額,提示輸入工資,不輸入就不讓繼續了,嘿嘿嘿!耍流氓了!
3.恩恩,購物開始,輸入對應的商品編號,否則,看提示!
4.購物途中可以隨時退出,退出時,打印你的購物清單,和賬號余額
同時把你的余額,和購物記錄,追加至userinfo.txt 文件中
5.第二次登陸,仍然需要輸入工資的,多少輸入一點吧,
6.接5,打印出你上次的購買清單,樣子有點醜,呵呵
總結:
這次作業用到的知識有,while 循環、for循環、if判斷、enumerate()、文件的讀取和寫入(列表到字符串的轉換)(字符串轉化為列表???)
裝B新技能 打印彩色字體,恩linux裏面也有格式(\033[31m加色字\033[0m)
好了,糾結了我兩天的程序就先這樣吧,我的態度很不好嗎?哎,書到用時方恨少啊。下次見!
Python實現購物車小程序