購物車程序啟動程序后,輸入用戶名密碼后,如果是第一次登錄,讓用戶輸入工資,然后打印商品列表
允許用戶根據商品編號購買商品用戶選擇商品后,檢測余額是否夠,夠就直接扣款,不夠就提醒
可隨時退出,退出時,打印已購買商品和余額在用戶使用過程中, 關鍵輸出,如余額,商品已加入購物車等消息,需高亮顯示
用戶下一次登錄后,輸入用戶名密碼,直接回到上次的狀態,即上次消費的余額信息。
默認用戶名:lhl 密碼:123456
程序流程圖:

#!/usr/bin/env python # -*- coding:utf-8 -*- #username=lhl,password=123456 #auth:lhl print("-------------歡迎登入xx購物廣場------------") usename=input("請輸入您的用戶名:") pwd=input("請輸入您的密碼:") if usename=='lhl' and pwd=='123456': with open('info.txt','r',encoding='utf-8') as f,\ open('price.txt','r',encoding='utf-8') as p: p=p.read() f=f.read() print('#################購物清單###############') print('%s'%f ,'剩余金額:%s'%p) product_list=[ ('iphone',5800), ('mac pro',9800), ('bike',800), ('watch',10600), ('coffee',31), ('python-book',85) ] shoping_list=[] salary=input("input your salary:") if salary.isdigit(): salary=int(salary) while True: for index,item in enumerate(product_list): print(index,item) user_choice=input("選擇商品>>:") if user_choice.isdigit(): user_choice=int(user_choice) if user_choice <len(product_list) and user_choice >=0: p_item=product_list[user_choice] if p_item[1] <=salary: shoping_list.append(p_item) salary-=p_item[1] print('添加 %s 到您的購物車,你的余額是:\033[0;31m%s\033[0m '%(p_item,salary)) else: print('你的余額還剩 \033[0;31m%s\033[0m 元,無法購買'%salary,'還差:\033[0;31m%s\033[0m元'%(p_item[1]-salary)) else: print("product code is not exits"%user_choice) elif user_choice=='q': print("---------shoping list--------------") for p in shoping_list: print(p) with open("info.txt",'a',encoding='utf-8') as f: f.write('商品:'+p[0]+'價格:') f.write(str(p[1])+'\n') print("您的余額是:\033[0;31m%s\033[0m "%salary) with open('price.txt','w',encoding='utf-8') as f: f.write(str(salary)) print("-------------歡迎再次光臨-------------") exit() else: print("invalid option")
