码迷,mamicode.com
首页 > 其他好文 > 详细

购物车

时间:2017-09-15 18:43:30      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:cts   写入   编号   []   open   lob   查询   lan   进入   

  1 # -*- coding:utf-8 -*-
  2 # author:Mr.chan
  3 
  4 """本代码实现的功能:
  5     1、启动程序后,显示查看购物车还是购买商品,然后让用户输入工资,再然后打印商品列表;
  6     2、允许用户根据商品编号购买商品;
  7     3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒;
  8     4、可随时退出,退出时,打印已购买的商品和余额,并把商品写入到文件中。
  9 """
 10 import sys
 11 
 12 def show_product_list():
 13     """展示商品列表 product_list """
 14     for index,item in enumerate(product_list):
 15         item_name = item[0]
 16         item_price = item[1]
 17         print("{0} >>> {1} {2}".format(index,item_name,item_price))
 18 
 19 def show_purchased():
 20     """显示购物车列表 shopping_cart(因为未结算,所以不在文件中查询已购买商品)"""
 21     print("Purchased products:".center(30, -))
 22     for k,v in shopping_cart:
 23         con = k +|+ v
 24         print(con)
 25     print("Your balance is %s\n" % salary)
 26 
 27 product_list = [
 28     (TCL TV,3500),
 29     (Android phone,3000),
 30     (Iphone 8,8000),
 31     (Lenovo computer,4000),
 32     ("Haier refrigerator",1500)
 33 ]
 34 
 35 def salary_isdigit():
 36     """判断输入的薪资是否是数字"""
 37     global salary  # 因为其他函数需要调用salar,所以不得不在这里定义为全局变量
 38     while True:
 39         salary = input("\nInput your salary:")
 40         if salary.isdigit():
 41             salary = int(salary)
 42             break
 43         else:
 44             print("Incorrect input\n")
 45 
 46 def shopping_list():
 47     """把购物车中的商品 shopping_cart 存放到文件 shopping_list.txt"""
 48     with open("shopping_list.txt",a) as f:
 49         for k,v in shopping_cart:
 50             temp = k +|+ v
 51             f.write(temp)
 52             f.write(\n)
 53 
 54 def check_shopping_list():
 55     """查看文件 shopping_list.txt 中已购买的商品"""
 56     while True:
 57         with open("shopping_list.txt", r) as f:
 58             for line in f:
 59                 if line.strip():
 60                     print(line.strip())
 61             break
 62 
 63 def check_or_buy():
 64     """查看文件shopping_list.txt中已购买的商品列表,还是购买商品"""
 65     while True:
 66         choice = input("1、查看购物车  2、购买商品\n(q=quit)请输入: ")
 67         if choice.isdigit():
 68             choice = int(choice)
 69             if choice == 1:
 70                 check_shopping_list()  # 调用购物车文件
 71                 break
 72             elif choice == 2:
 73                 break   # 结束本次循环,进入下一循环
 74             else:
 75                 print("Incorrect input!")
 76         elif choice == "q":
 77             sys.exit("欢迎下次光临,请慢走!")
 78         else:
 79             print("Incorrect input!")
 80 
 81 def buy():
 82     while True:
 83         global salary # 在salary_isdigit()这个函数里面定义了salary,所以需要用global来引用它
 84         print("product list".center(30, -))
 85         show_product_list()  # 调用函数,展示商品列表
 86         choice = input("[c=check,q=quick]Do you want to buy? ")
 87         if choice.isdigit():
 88             choice = int(choice)
 89             if choice >= 0 and choice < len(product_list):
 90                 p_item = product_list[choice]  # 选择的商品
 91                 if int(p_item[1]) <= salary:  # 当价格小于salary就加入购物车
 92                     shopping_cart.append(p_item)
 93                     salary -= int(p_item[1])  # 扣钱
 94                     print("Add %s to shopping cart,and your balance is %s\n" % (p_item, salary))
 95                 else:
 96                     print("Your balance is %s, not enough!\n" % salary)
 97             else:
 98                 print("Product is not exist!\n")
 99         elif choice == c:
100             show_purchased()  # 调用函数
101         elif choice == q:
102             show_purchased()  # 调用函数
103             shopping_list()   # 保存商品到文件中
104             exit()
105         else:
106             print("Incorrect input, please try again!\n")
107 
108 
109 shopping_cart = []
110 
111 def main():
112     while True:
113         check_or_buy()  # 调用函数,查看购物车还是购买商品
114         salary_isdigit()   # 调用函数,判断salary是否数字,如果是则进行下一步
115         buy()  # 调用函数,购买商品
116         # check_shopping_list()
117 
118 
119 
120 
121 if __name__ == __main__:
122     main()

 

购物车

标签:cts   写入   编号   []   open   lob   查询   lan   进入   

原文地址:http://www.cnblogs.com/relax1949/p/7527329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!