购物车小程序练习

 1 product = [
 2 ('羽绒服',200),
 3 ('毛衣',80),
 4 ('衬衫',50),
 5 ('牛仔裤',120),
 6 ('t恤',55),
 7 ('苹果笔记本',8000)]
 8 shop_car = []
 9 print ("欢迎来到购物商城!".center(50,'-'))
10 salary = 5000
11 print ("您好,您的账户余额为:\033[32;1m[%s]\033[0m元,如需充值请输入p,继续购物请输入j:"% salary)
12 t = False
13 while not t:
14     choice = input()
15     if choice == 'p':
16         salary_add = (input("请输入您需要充值的金额:"))
17         if salary_add.isdecimal():
18             salary += int(salary_add)
19             print ("您好,您的账户余额为:\033[32;1m[%s]\033[0m元,如需充值请输入p,继续购物请输入j:"% salary)
20         else:
21             print ("请输入正确的充值金额!")
22     elif choice == 'j':
23         t = True
24     else:
25         print ("请输入正确的选项!")
26 y = False        
27 while not y:    
28     print ("商品列表".center(50,'-'))           #打印商品列表
29     for item in enumerate(product):
30         index = item[0]
31         p_name = item[1][0]
32         p_price = item[1][1]
33         print (index,'.',p_name,p_price)
34     user_choice = input("退出请输入q,查看已购买商品请输入c,请输入序号选择您想要购买的商品:")
35     if user_choice.isdecimal():                   #判断用户输入的是否是数字
36         user_choice = int(user_choice)         
37         if user_choice < len(product):            #判断商品序号是否存在
38             p_item = product[user_choice]
39             if p_item[1] <= salary:               #判断用户是否能够买得起
40                 shop_car.append(p_item)
41                 salary -= p_item[1] 
42                 print("您已购买了\033[32;1m[%s]\033[0m,您的账户余额为\033[32;1m[%s]\033[0m元"% (p_item,salary))
43             else:
44                 print("您的账户余额不足,如需充值请输入p,选择其它商品请输入j:")
45                 t = False
46                 while not t:
47                     choice = input()
48                     if choice == 'p':
49                         salary_add = (input("请输入您需要充值的金额:"))
50                         if salary_add.isdecimal():
51                             salary += int(salary_add)
52                             print ("您好,您的账户余额为:\033[32;1m[%s]\033[0m元,如需充值请输入p,继续购物请输入j:"% salary)
53                         else:
54                             print ("请输入正确的充值金额!")
55                     elif choice == 'j':
56                         t = True
57                     else:
58                         print ("请输入正确的选项!")
59         else:
60             print("商品列表中无该商品!")
61     elif user_choice == 'q':
62         exit("欢迎下次光临!")
63     elif user_choice == 'c':
64         print ("您已购买了\033[32;1m[%s]\033[0m,您的账户余额为\033[32;1m[%s]\033[0m元"% (shop_car,salary))
65     else:
66         print("请输入正确的序号!")
posted @ 2016-11-21 22:13  废柴米老鼠  阅读(265)  评论(0编辑  收藏  举报