python之作业--------购物车优化
Read Me:继上次简单购物车的实现,有再一次的升级优化了下,现实现以下几个功能:
1.有客户操作和商家操作,实现,客户可以买东西,当金额不足提醒,最后按q退出,打印购物车列表
2.商家可以添加操作、修改商品价格操作
3.此次所有的商品信息存储在product.txt文件中例:Watch: 1000,这样的形式存在。
4. 运行此程序需要有商品信息的txt文件如上,按提示操作即可
思维导图:
代码如下:
- 1 #!/usr/bin/env python
- 2 #-*- Coding:utf-8 -*-
- 3 # Author:Eric.Shen
- 4 #2018.02.06
- 5 #path python3.5
- 6 #优化版的购物车
- 7 #用户入口:
- 8 #1.商品的信息存到文件里
- 9 #2.已购商品,余额记录
- 10 #商家入口:
- 11 #1.可以添加商品 2.修改商品价格
- 12 #存储商品列表
- 13 import fileinput
- 14 product_list = []
- 15 f = open("product.txt","r")#打开文件
- 16 for line in f.readlines():
- 17 line =line.strip()#去掉最后一个换行符
- 18 index,item = line.split(":")#以冒号分割得到前后两个数据
- 19 product_list.append((index,item))#添加的数据
- 20 f.close()
- 21
- 22 def print_product_list():
- 23 for index,item in enumerate(product_list):
- 24 print(index,item)
- 25 #用户入口
- 26 #用户购物
- 27 def user_shopping():
- 28 salary = input("请输入你的薪水:")
- 29 print_product_list()
- 30 if salary.isdigit():
- 31 salary = int(salary)
- 32 shopping_list = []#存放用户购物车清单
- 33 while True:
- 34 option = input("喜欢那个就买哪个(对应的标号):")
- 35 if option.isdigit():
- 36 option = int(option)
- 37 if option >= 0 and option <= len(product_list):
- 38 p_item = product_list[option]#用户选择的商品
- 39 #print(product_list)
- 40 #print(p_item[1])
- 41 c_num = int(p_item[1])
- 42 if salary >= c_num:
- 43 shopping_list.append(p_item)
- 44 salary -= c_num
- 45 print("添加购物车成功,你的余额还有%s"%(salary))
- 46 else:
- 47 print("你的余额不足,只剩%s元"%(salary))
- 48 else:
- 49 print("输入错误,请重新输入!")
- 50 elif option == "q":
- 51 print("----------------购物清单---------------")
- 52 for s_list in shopping_list:
- 53 print(s_list)
- 54 print("你的余额为%s"%(salary))
- 55 print("..........exit.........")
- 56 exit()
- 57 else:
- 58 print("无效的输入")
- 59
- 60 #商家入口
- 61 #商家添加商品
- 62 def add_product():
- 63 name_of_product = input("请输入你要添加的商品名字:")
- 64 price_of_product = input("请输入你要添加商品的价格:")
- 65 f = open("product.txt","a")
- 66 f.write(str("\n"+name_of_product)+": %s"%(price_of_product))
- 67 f.close()
- 68 print("添加成功!\nexit----------")
- 69
- 70
- 71
- 72 #修改商品价格
- 73 def change_price():
- 74 print_product_list()#打印商品列表
- 75 choice = input("请输入你的选择:")
- 76 #name_of_change = input("请输入你要改变的商品名字")
- 77 price_of_change = input("请输入你要改变商品的价格:")
- 78 if choice.isdigit():
- 79 choice = int(choice)
- 80 if choice >=0 and choice <= len(product_list):
- 81 p_item = product_list[choice]#选择的商品
- 82 #c_num = int(p_item[1])#转换成int类型
- 83 for line in fileinput.input("product.txt",inplace = "%s"%(choice)):#对输入的选择行进行修改
- 84 line = line.replace("%s"%(p_item[1]),"%s"%(price_of_change)).strip()
- 85 print(line)
- 86 exit("修改成功!")
- 87 else:
- 88 print("输入无效")
- 89 else:
- 90 if choice == "q":
- 91 exit("退出")
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105 if __name__ == "__main__":
- 106 print("--------------------------"
- 107 "--------------------------"
- 108 "\n"
- 109 " 欢迎进入购物菜单 "
- 110 "\n"
- 111 "\n"
- 112 "商家请按b,用户请按c\n"
- 113 "--------------------------"
- 114 "--------------------------")
- 115 c_num = input("请输入你的选择:")#使用者选择
- 116 if c_num == "b":
- 117 print("--------------------------"
- 118 "--------------------------"
- 119 "\n"
- 120 " 欢迎进入商家界面 "
- 121 "\n"
- 122 "\n"
- 123 "添加商品请按a,修改价格请按c\n"
- 124 "--------------------------"
- 125 "--------------------------")
- 126 c_num2 = input("请输入你的选择:")
- 127 if c_num2 == "a":
- 128 #实现添加商品功能
- 129 add_product()
- 130 if c_num2 == "c":
- 131 #实现商品价格修改功能
- 132 change_price()
- 133 else:
- 134 print("输入有误!")
- 135 if c_num == "c":
- 136 print("--------------------------"
- 137 "--------------------------"
- 138 "\n"
- 139 " 欢迎进入用户界面 "
- 140 "\n"
- 141 "\n"
- 142
- 143 "--------------------------"
- 144 "--------------------------")
- 145 #购物功能
- 146 user_shopping()
- 147 else:
- 148 print("输入有误程序退出!")
View Code
结束:有什么错误欢迎指出来,欢迎打扰 -_-