购物车完善版,客户余额及所购商品保存在文件
生活随笔
收集整理的這篇文章主要介紹了
购物车完善版,客户余额及所购商品保存在文件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
購物車優化:
用戶入口:
商品信息存在文件里;
已購商品,余額記錄保存在文件里,下次運行程序要讀取上一次運行后的余額信息
商家入口:
可以添加商品,修改商品價格
文件:
1. customer.txt
3220
{'shampoo': '1', 'iphone 8': 2, 'iphone X': 2, 'starbucks latte': 6, 'bread': 13, 'book': 12, 'MacPro': 1}
2. goods.txt
{"MacPro":"12000","iphone 8":"8000","iphone X":"10000","starbucks latte":"25","book":"50","bread":"5","shampoo":"25"}
3. index.py
?
import json with open("goods.txt",'r') as f:contents = f.read() goods_list = json.loads(contents) # convert to dict type 獲取商品列表,字典格式 with open("customer.txt",'r') as f:contents = f.readlines() customer_lines = list() for line in contents:customer_lines.append(line.strip()) salary = int(customer_lines[0]) # 分別獲取余額和已購買的商品 items_bought = eval(customer_lines[1]) # 字符串轉換成字典 confirm = input("Your balance is {balance}, do you want to add more money? 'y' to add".format(balance = salary)) # 要不要加錢 if confirm == 'y':add_salary = int(input("Money you wanna add: "))salary = salary + add_salary # 更新余額print("Your latest balance is {salary}".format(salary = salary))available_goods = list() # available_goods 保存的是根據salary可以買的商品 for i in goods_list:if salary >= int(goods_list[i]):available_goods.append(i) if len(available_goods) == 0:print("You have no enought money, now exit.") # 沒有可以購買的商品,直接退出 exit() print("Goods for you:") goods_order = 1 for j in available_goods: # 一行一個打印出可以購買的商品print("{goods_order}: {j}, price: {goods}".format(goods_order=goods_order,j=j, goods=goods_list[j]))goods_order += 1 # print(len(available_goods)) # 有多少個商品可以買 chose_list = dict() total_price = 0 total_price_tmp = 0 while True:choose_order = int(input("Please choose an item:"))while choose_order > len(available_goods) or choose_order <= 0: # 如果可購買商品列表范圍,則提示用戶直到輸入的order存在為止choose_order = int(input("The item you selected not exsit, try again:"))choose_order_num = int(input("How many do you want?"))if available_goods[choose_order-1] in chose_list:chose_list[available_goods[choose_order - 1]] = chose_list[available_goods[choose_order-1]] + choose_order_numelse:chose_list[available_goods[choose_order-1]] = choose_order_numprint("Items you chose:")for i in chose_list: # 打印出本次選擇的商品及數量print(i,':',chose_list[i])for k in chose_list: # 計算本次選購商品的總價格total_price_tmp = total_price_tmp + int(goods_list[k])*chose_list[k]total_price = total_price_tmptotal_price_tmp = 0if salary < total_price: # 如果超過余額,則清空重選print("The total price of your items is {total_price}".format(total_price=total_price))print("You don't have enought balance. please try again.")chose_list.clear()total_price = 0else:print("The total price of your items is {total_price}".format(total_price=total_price))buy_again = input("'c'to calculate, 'q' to cancel, other key to continue:") # 繼續不if buy_again == "q":print("Cancel all. Your balance is {salary}".format(salary=salary))breakelif buy_again == 'c': # 選c 計算商品價格balance = salary - total_priceif balance == salary:print("Your balance is {balance}, you didn't buy anything.".format(balance=balance))else:print("Your balance is {balance}, the items you bought this time are blow:".format(balance=balance))for item in chose_list:print("{item}, amount: {count}, cost {price}".format(item = item, count = chose_list[item], price = int(chose_list[item]) * int(goods_list[item])))break for i in chose_list: # 更新購買的商品字典if i in items_bought:items_bought[i] += chose_list[i]else:items_bought.update(chose_list) print("All the items you bought are blow:") for i in items_bought:print(i,':',items_bought[i]) f = open("customer.txt","w") # 更新customer.txt文件 f.write(str(balance)+'\r') f.write(str(items_bought)) f.close()?
轉載于:https://www.cnblogs.com/jnyy0/p/9236126.html
總結
以上是生活随笔為你收集整理的购物车完善版,客户余额及所购商品保存在文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 类variant解剖
- 下一篇: 【转载】大叔推荐博客索引