购物车程序更新:

更新商家入口,实现以下功能:

1. 商家能够修改商品价格;

2. 商家能够下线商品;

3. 商家能够增加商品;

4. 商品信息存在文件中

 # -*- coding:utf-8 -*-
# LC
product_list = []
product_line = []
count = 0 with open("product_list","r") as f: #打开商家货品清单文件
for item in f:
product_line = item.rstrip().split(",") #将每行赋值给成一个列表,以“,”区分
product_list.append(product_line) #将每行的列表加入product_list列表中 print("This is your production".center(50,"-")) #打印列表
for i in product_list:
print(count,i)
count+=1 #提供增加商品,修改商品价格,删除商品功能
while True:
select = input("Please input you selection,'a' to add,'m' to modified the price,'d' to Delete,'q' to quit :")
count = 0
if select == 'a':
add_product = input("Please input new product name:")
add_price = input("Please input new product price:")
new_product = [add_product,add_price] #将新添加的商品赋值成列表
product_list.append(new_product) #向产品清单列表中插入新添加的商品
with open("product_list","a") as f_add: #将添加的商品写入文件中,以追加的方式
f_add.writelines(product_list[-1][0]+","+product_list[-1][1]+"\n")
print("\033[1;31mThis is your new production list\033[0m".center(50,"-"))
for i in product_list: #打印新的产品清单
print(count,i)
count+=1
elif select == 'm':
modify_index = input("Please input index of the item which you want to modify:") #输入要修改价格的产品序号
modify_index = int(modify_index)
length = product_list.index(product_list[-1]) #判断输入的修改价格产品序号是否再产品清单范围
if 0 <= modify_index <= length:
new_price = input("Please input the new price:") #输入新的价格
product_list[modify_index][1]=new_price #将新的价格修改至产品清单列表中
with open("product_list","w") as f_modify: #想修改的产品清单列表写入文件中
for i in product_list:
f_modify.writelines(i[0]+","+i[1]+"\n")
print("\033[1;31mThis is your new production list\033[0m".center(50,"-"))
for i in product_list: #打印新的产品清
print(count,i)
count+=1
else:
print("Your select is invalid, Please try again!") #如果输入的序号不在范围内,则报错重新来
elif select == 'd': #删除商品
delete_index = input("Please input index of the item which you want to delete:")
delete_index = int(delete_index)
length = product_list.index(product_list[-1]) #判断输入的删除序号是否再产品清单范围
if 0 <= delete_index <= length:
product_list.pop(delete_index) #从产品清单中删除制定的产品
with open("product_list","w") as f_del:
for i in product_list:
f_del.writelines(i[0]+","+i[1]+"\n") #将新的产品清单写入文件中
print("\033[1;31mThis is your new production list\033[0m".center(50,"-"))
for i in product_list:
print(count,i)
count+=1
else:
print("Your select is invalid, Please try again!") elif select == "q":
print("You have been quit!")
break
else:
print("invalid input,please input again!")

商家入口程序

pic,2
rice,3
water,32
phone,5000
pia,300

货品清单文件

更新用户购买程序,实现以下功能:

1. 用户第一次购买输入薪资;

2. 用户购买商品信息写入文件;

3. 用户再次购买先读取之前的购买清单文件,查看余额;

 # -*- coding:utf-8 -*-
# LC
import os
product_list = [] #定义商品清单列表
product_line = [] #定义商品清单中每行的列表
user_buy_list = [] #定义用户已经购买商品列表
user_buy_product = [] #定义用户购买单个商品的列表
with open("product_list","r") as f_production: #读取商品清单列表
for item_production in f_production:
product_line = item_production.rstrip().split(",")
product_list.append(product_line) if os.path.getsize("shopping_list") == 0: #判断是否为第一次消费,如果消费清单文件为空,则第一次消费
salary = input("Please input your salary:")
balance = int(salary) #将余额定义为薪资
else:
length = len(open("shopping_list","rU").readlines()) #如果不是第一次消费,计算消费清单文件行数,提取消费列表
count = 0
with open("shopping_list", "r") as f_shopping_list:
for shopping_item in f_shopping_list:
count +=1
if 2 <= count <= length-2: #去除第一行和最后两行,将消费清单中已购买的商品提取出来
shopping_item = shopping_item.strip().split() #将购买的商品转换为列表,包含商品,单价,数量
user_buy_list.append(shopping_item) #将已经购买的商品记录至用户消费清单列表中
elif count == length:
shopping_item = shopping_item.strip().split()
balance = int(shopping_item[-1]) #如果非第一次消费,则余额为消费清单文件中的余额,提取出来
while True:
print("This is the production list".center(50, "-"))
count = 0
for i in product_list:
print(count, i)
count += 1
user_select = input("Your balance is %d, 's' to start to shopping,'q' to quit : "%(balance))
if user_select == 's':
user_buy_product = []
user_select_index = input("Please select the index of the one you want to buy:") #输入要购买的商品
user_select_count = input("Please input the number how many you want to buy:") #输入要购买的商品数量
user_select_index = int(user_select_index)
user_select_count = int(user_select_count)
unit_price = int(product_list[user_select_index][1]) #要购买的商品单价
product_price = unit_price * user_select_count #购买的商品金额,即单价乘以总价
if product_price <= balance:
user_buy_product.append(product_list[user_select_index][0]) #将购买的商品的名称插入新的单个列表中
user_buy_product.append(product_list[user_select_index][1]) #将购买的商品的单价插入新的单个列表中
user_buy_product.append(str(user_select_count)) #将购买的商品的数量插入新的单个列表中
user_buy_list.append(user_buy_product) #将新购买的商品,包含名称,单价,数量插入用户购买清单中
balance = balance - product_price #将金额出去已经购买的商品金额,以免余额不足
else: #购买的商品和数量,余额不足
print("\033[1;31mYour choice is out of your money!\033[0m")
continue
elif user_select == 'q':
break
else:
print("invalid input ,try again")
continue #统计用户购买商品的总金额和消费余额
total_price = 0
for i in user_buy_list:
user_buy_product_price = int(i[1]) * int(i[2]) #用户购买的单个商品的总额,即单件商品的单价乘以数量
total_price = total_price + user_buy_product_price #统计消费总金额
total_consumption = ["Total_Consumption:"]
balance_list = ["Your balance:"] #消费清单上记录余额信息
balance_list.append(balance)
total_consumption.append(total_price)
user_buy_list.append(total_consumption) #将总消费金额信息插入用户消费清单中
user_buy_list.append(balance_list) #将余额信息插入用户消费清单中 #将用户购买的商品写入消费清单文件中,并记录总价和余额
with open("shopping_list","w") as f_buy:
f_buy.writelines("product".center(15,"-")+"price".center(15,"-")+"count".center(15,"-")+"\n")
for i in user_buy_list:
if len(i) == 3: #将商品写入
f_buy.writelines(str(i[0]).center(15," ")+str(i[1]).center(15," ")+str(i[2]).center(15," ")+"\n")
else: #将余额和消费总额写入
f_buy.writelines(str(i[0]).ljust(30, " ") + str(i[1]).center(15, " ") + "\n") #打印最终购买清单
print("This is your purchase list".center(45,"-"))
with open("shopping_list","r") as f_read:
for i in f_read:
print(i.strip("\n"))

用户端程序

购买清单文件

----product---------price----------count-----
pic 2 20
rice 3 50
water 32 300
phone 5000 2
pia 300 50
phone 5000 2
phone 5000 1
Total_Consumption: 49790
Your balance: 210

购买清单文件

python---购物车---更新的更多相关文章

  1. 简单的python购物车

                 这几天,一直在学python,跟着视频老师做了一个比较简单的python购物车,感觉不错,分享一下 products = [['Iphone8',6888],['MacPro ...

  2. python 购物车小程序

    python 购物车小程序 功能要求:1.启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表2.允许用户根据商品编号购买商品3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒4. ...

  3. python购物车小案例

    python购物车小案例# 案列描述:有一个小型水果店里面有水果(苹果:¥8/kg,香蕉:¥5/kg,芒果:¥15/kg,葡萄:¥12/kg),客户带了100元钱进店选购水果.# 1.客户输入相应序号 ...

  4. Python字符串更新

    Python字符串更新:截取字符串的某一部分 和 其他字符串进行拼接. 注:可以修改字符串的值,但修改的不是内存中的值,而是创建新的字符串. 1.使用字符串常量进行更新: # 使用字符串常量 strs ...

  5. python 购物车和三级菜单

    程序:购物车程序 需求: 启动程序后,让用户输入工资,然后打印商品列表 允许用户根据商品编号购买商品 用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 可随时退出,退出时,打印已购买商品和余额 ...

  6. 如何在Anaconda中把python环境更新更高版本

    把Anaconda中的python从3.5.5更新到3.6版本,不想卸载重新安装.办法如下: 开始->Anaconda Promot 在Anaconda Promot中,输入: conda up ...

  7. 5th,Python购物车模拟

    1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 4.可随时退出,退 ...

  8. Python - 常用更新命令以及常见库安装

    库的安装方式一般有两种: 一. pip直接安装(或使用豆瓣源) pip install scrapy pip install -i https://pypi.douban.com/simple/ sc ...

  9. Python购物车

    product_list = [ ['Iphone',5888], ['Mac Air',8000], ['XiaoMi',19.9], ['coffee',30], ['Tesla',820000] ...

  10. python 购物车小程序(列表、循环、条件语句)

    goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...

随机推荐

  1. 深入浅出EF之ModelFirst和DBFirst

    在上篇博文中,小编主要简单的介绍了一下EF的一些基础知识,其中,小编蜻蜓点水的提了一下ModelFirst和DBFirst,ModelFirst先设计实体,然后根据模型生成数据库,DBFirst根据数 ...

  2. java造成内存泄露原因

    一.Java内存回收机制  不论哪种语言的内存分配方式,都需要返回所分配内存的真实地址,也就是返回一个指针到内存块的首地址.Java中对象是采用new或者反射的方法创建的,这些对象的创建都是在堆(He ...

  3. visual studio 2015使用python tools远程调试maya 2016

    步骤: 1. 去https://apps.exchange.autodesk.com/MAYA/en/Home/Index搜索Developer Kit并下载,maya 2016可以直接点击这里下载. ...

  4. SQL Server性能优化与管理的艺术 附件下载地址

    首先感谢读者们对鄙人的支持,购买了<SQL Server性能优化与管理的艺术>,由于之前出版社的一些疏忽,附件没有上传成功,再次本人深表歉意. 请需要下载附件的读者从下面链接下载,谢谢: ...

  5. Java 8新特性探究(五)Base64详解

    BASE64 编码是一种常用的字符编码,在很多地方都会用到.但base64不是安全领域下的加密解密算法.能起到安全作用的效果很差,而且很容易破解,他核心作用应该是传输数据的正确性,有些网关或系统只能使 ...

  6. Linux Debugging(一): 使用反汇编理解C++程序函数调用栈

    拿到CoreDump后,如果看到的地址都是????,那么基本上可以确定,程序的栈被破坏掉了.GDB也是使用函数的调用栈去还原"事故现场"的.因此理解函数调用栈,是使用GDB进行现场 ...

  7. maven中去掉单元测试的配置

    如果是在命令行中去掉测试,可以在命令行中输入:mvn install -Dmaven.test.skip=true 在pom.xml <plugins>       <plugin& ...

  8. Dynamics CRM 2011 仪表盘(dashbord)中加入公告(announcement)模块

    具体步骤如下: 1.将一下代码黏贴入一个取名叫"announcementsondashboard.htm"的html文件中,当然文件名你随便起无所谓. <span style ...

  9. C++虚拟多重继承对象模型讨论

    C++虚拟多重继承对象模型讨论 作者:magictong 调试环境:Windows7VS2005 概述 记得刚开始写C++程序时,那还是大学时光,感觉这玩意比C强大多了,怎么就实现了多态,RTTI这些 ...

  10. STL - 各个容器的使用时机

    deque的使用场景:比如排队购票系统,对排队者的存储可以采用deque,支持头端的快速移除,尾端的快速添加.如果采用vector,则头端移除时,会移动大量的数据,速度慢. vector与deque的 ...