python练习之购物车脚本


 1 # -*- coding:utf-8 -*-
#简单的购物车小程序 author:李瑞鑫
2 shopping_car =[] 3 product_list_tatol = "---product list----" 4 welcome = "-----------welcome to shopping marketi----------" 5 product_list = [ 6 ('iphone',5888), 7 ('lenovo',998), 8 ('cup',12), 9 ('thinkpad',4500), 10 ('notebook',6) 11 ] 12 print welcome 13 salary = raw_input("input your salary:>>>") 14 if salary.isdigit(): 15 salary = int(salary) 16 while True: 17 print product_list_tatol 18 for item in product_list: 19 print product_list.index(item)+1,item 20 choice = raw_input("choice you want to buy:>>> ___ 0 to exit") 21 if choice.isdigit(): 22 choice = int(choice) 23 if choice > 5 or choice < 0: 24 print "no this goods,请重新选择" 25 continue 26 elif choice <=5 and choice >=1: 27 if salary < product_list[choice-1][1]: 28 print "账户余额不足,请购买其他商品或者退出" 29 continue 30 else: 31 pass 32 item_choice = product_list[choice-1] 33 shopping_car.append(item_choice) 34 print "you buy goods is", shopping_car 35 salary = salary - product_list[choice-1][1] 36 print "your balance is %d"%(salary) 37 38 elif choice == 0: 39 print "you buy goods is",shopping_car, 40 print "your balance is %d"%(salary) 41 exit()



 2.带有身份认证的购物车程序

 1 # -*- coding:utf-8 -*-
 2 import time, os
 3 shop_car3 = []
 4 shop_car2 = []
 5 user_list = []
 6 count = 0
 7 user = "liruixin"
 8 password = "123456"
 9 raw_user = raw_input("username:")
10 f = open("user.txt", 'a+')
11 lines = []
12 for lines in f.readlines():
13     lines = lines.replace("\n", "").split(",")
14 f.close()
15 for i in lines:
16     if i == raw_user:
17         print "this user is locked, please request to root"
18         exit()
19     else:
20         pass
21 while count < 3:
22     if raw_user == user:
23         raw_password = raw_input("password:")
24         count = count + 1
25         if count == 3:
26             user_list.append(raw_user)
27             user = open('user.txt', 'a')
28             for raw_user in user_list:
29                 user.write(raw_user + ',')
30             user.close()
31         else:
32             pass
33         if raw_password == password:
34             print "success"
35             a = time.localtime()
36             b = time.strftime("%Y-%m-%d %H:%M:%S", a)
37             title = "welcome to shopping"
38             print b.center(40, "*")
39             print title.center(40, "*")
40             shop_car = []
41             goods_list = [
42                 ["iphone 7", 5888],
43                 ["coffe", 30],
44                 ["computer", 3999],
45                 ["mac pro", 12000]
46             ]
47             filename = r'salary.txt'
48             if os.path.exists(filename):
49                 temp = open("salary.txt", "r")
50                 for i in temp:
51                     salary = i
52                     print "您的卡上余额为", salary
53             else:
54                 salary = raw_input("请输入充值金额:")
55 
56             if salary.isdigit():
57                 salary = int(salary)
58             while True:
59                 print "******* good list **********"
60                 for i in enumerate(goods_list):
61                     print i[0] + 1, i[1][0], i[1][1]
62                 choice = raw_input("choice goods __  quit to exit:")
63                 if choice.isdigit():
64                     choice = int(choice)
65                 if choice <= 4 and choice >= 1:
66                     if salary >= goods_list[choice - 1][1]:
67                         salary = salary - goods_list[choice - 1][1]
68                         price = (goods_list[choice - 1][1])
69                         item = (goods_list[choice - 1][0])
70                         print "你购买了  [\033[32;1m%s\033[0m]   花费了 [\033[31;1m%d\033[0m] , 你的余额为 [\033[31;1m%d\033[0m]" % (
71                             item, price, salary)
72                         shop_car.append(item)
73                         shop_car2.append(price)
74                     else:
75                         print "你的余额不足,请选择其他商品"
76                         continue
77                 elif choice == "quit":
78                     print "你购买的商品如下"
79                     a = set(shop_car)
80                     for i in a:
81                         print i,shop_car.count(i)
82                     b = sum(shop_car2)
83                     print "总共消费了 \033[32;1m%d\033[0m 元钱"%b
84                     print "你的余额为\033[31;1m%d\033[0m 元" % salary
85                     print "欢迎下载再来,拜拜"
86                     salary_file = open("salary.txt", "w")
87                     salary_file.write("%d" % salary)
88                     salary_file.close()
89                     exit()
90                 else:
91                     print "没有该商品,请重新输入"
92                     continue
93         else:
94             print "this password error"
95     else:
96         print "no this user"
97         exit()

 3.带有充值功能的购物车

  1 # -*- coding:utf-8 -*-
  2 import time, os
  3 shop_car3 = []
  4 shop_car2 = []
  5 user_list = []
  6 yue = []
  7 count = 0
  8 user = "liruixin"
  9 password = "123456"
 10 message = "欢迎光临天猫超市,请登录"
 11 print message.center(50,"*")
 12 raw_user = raw_input("username:")
 13 f = open("user.txt", 'a+')
 14 lines = []
 15 for lines in f.readlines():
 16     lines = lines.replace("\n", "").split(",")
 17 f.close()
 18 for i in lines:
 19     if i == raw_user:
 20         print "this user is locked, please request to root"
 21         exit()
 22     else:
 23         pass
 24 while count < 3:
 25     if raw_user == user:
 26         raw_password = raw_input("password:")
 27         count = count + 1
 28         if count == 3:
 29             user_list.append(raw_user)
 30             user = open('user.txt', 'a')
 31             for raw_user in user_list:
 32                 user.write(raw_user + ',')
 33             user.close()
 34         else:
 35             pass
 36         if raw_password == password:
 37             pass
 38 
 39 
 40 
 41             a = time.localtime()
 42             b = time.strftime("%Y-%m-%d %H:%M:%S", a)
 43             title = "登录成功,正在跳转"
 44             print b.center(40, "*")
 45             print title.center(40, "*")
 46             shop_car = []
 47             goods_list = [
 48                 ["iphone 7", 5888],
 49                 ["coffe", 30],
 50                 ["computer", 3999],
 51                 ["mac pro", 12000]
 52             ]
 53             filename = r'salary.txt'
 54             if os.path.exists(filename):
 55                 temp = open("salary.txt", "r")
 56                 for i in temp:
 57                     salary = int(i)
 58 
 59                     print "您的卡上余额为", salary
 60                     chongzhi = raw_input("按a充值,按b进入商城")
 61                     if chongzhi =="b":
 62                         pass
 63                     if chongzhi =="a":
 64                         jine = int(raw_input("请输入充值金额:"))
 65                         sums = int(i)+jine
 66                         salary = sums
 67                         print "充值成功,最新余额为 \033[32;1m%d\033[0m "%sums
 68                         g = open("salary.txt","w")
 69                         g.write(str(sums))
 70                         g.close()
 71             else:
 72                 salary = raw_input("请输入充值金额:")
 73 
 74                 if salary.isdigit():
 75                     salary = int(salary)
 76             while True:
 77                 print "******* good list **********"
 78                 for i in enumerate(goods_list):
 79                     print i[0] + 1, i[1][0], i[1][1]
 80                 choice = raw_input("choice goods __  quit to exit:")
 81                 if choice.isdigit():
 82                     choice = int(choice)
 83                 if choice <= 4 and choice >= 1:
 84                     if salary >= goods_list[choice - 1][1]:
 85                         salary = salary - goods_list[choice - 1][1]
 86                         price = (goods_list[choice - 1][1])
 87                         item = (goods_list[choice - 1][0])
 88                         print "你购买了  [\033[32;1m%s\033[0m]   花费了 [\033[31;1m%d\033[0m] , 你的余额为 [\033[31;1m%d\033[0m]" % (
 89                             item, price, salary)
 90                         shop_car.append(item)
 91                         shop_car2.append(price)
 92                     else:
 93                         rr = raw_input("你的余额不足,按y充值:按q返回商品列表")
 94                         if rr == "y":
 95                             r = int(raw_input("请输入充值金额:"))
 96                             summ = int(salary)+r
 97                             print "充值成功,最新余额 \033[32;1m%d\033[0m"%summ
 98                             salary = summ
 99                             o = open("salary.txt","w")
100                             o.write(str(summ))
101                             o.close()
102                         elif rr =="q":
103                             continue
104 
105 
106                 elif choice == "quit":
107                     print "你购买的商品如下"
108                     a = set(shop_car)
109                     for i in a:
110                         print i,shop_car.count(i)
111                     b = sum(shop_car2)
112                     print "总共消费了 \033[32;1m%d\033[0m 元钱"%b
113                     print "你的余额为\033[31;1m%d\033[0m 元" % salary
114                     print "欢迎下次再来,拜拜"
115                     salary_file = open("salary.txt", "w")
116                     salary_file.write("%d" % salary)
117                     salary_file.close()
118                     exit()
119                 else:
120                     print "没有该商品,请重新输入"
121                     continue
122         else:
123             print "this password error"
124     else:
125         print "no this user"
126         exit()

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM