Read Me:继上次简单购物车的实现,有再一次的升级优化了下,现实现以下几个功能:

               1.有客户操作和商家操作,实现,客户可以买东西,当金额不足提醒,最后按q退出,打印购物车列表

               2.商家可以添加操作、修改商品价格操作

               3.此次所有的商品信息存储在product.txt文件中例:Watch: 1000,这样的形式存在。

               4. 运行此程序需要有商品信息的txt文件如上,按提示操作即可

思维导图:

 

代码如下:

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

View Code

 

 

结束:有什么错误欢迎指出来,欢迎打扰  -_-

posted on 2018-02-06 11:39 Eric.Shen 阅读() 评论() 编辑 收藏

版权声明:本文为zhengyuan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zhengyuan/p/8421539.html