改购物车
1 __author__ = 'ZZG'
2
3 product_list =[
4 ["iphone",5300],
5 ["book",55],
6 ["watch",2200],
7 ["bike",950],
8 ["pc",11000],
9 ["VR glass", 1000]
10 ]
11 #將產品列表寫入文件中
12 with open('product_list.txt','w')as pro:
13 for i in product_list:
14 pro.write(str(i))
15 pro.write("\n")
16 pro.close()
17 entrance = input("選擇用戶入口還是商家入口:")
18 exit_flag = True
19 shopping_list = []
20
21 #判斷入口
22 if entrance == "用戶入口":
23 salary = input("請輸入你的工資:")
24
25 while exit_flag:
26 with open("salary.txt",'w+')as sa:
27 sa.write(str(salary))
28 sa.close()
29 #判斷工資是否為數字
30 if str(salary).isdigit():
31 salary = int(salary)
32 if salary > 0:
33 #讀取并答應文件中的商品列表
34 with open('product_list.txt')as p:
35 for index ,products in enumerate(p):
36 print(index,products)
37 #用戶輸入購買商品的序號
38 number_of_product = input("\n請輸入商品的序號")
39 #判斷輸入的類型
40 if number_of_product.isdigit():
41 number_of_product = int(number_of_product)
42 #判斷輸入的商品序號是否存在
43
44 if number_of_product < index+1 and number_of_product >= 0:
45 products = product_list[number_of_product]
46
47 if salary > products[1]:
48 salary = salary - products[1]
49 shopping_list.append(products)
50 print('已購買商品列表'.center(50,'-'))
51 for i1 in shopping_list:
52 print(i1)
53 print("余額為\033[032;1m%s\033[0m"%( salary))
54 with open('shoping_list.txt','a+')as sl:
55 sl.write(str(shopping_list))
56 sl.close()
57 continue
58 else:
59 print("你的余額為\033[031;1m%s\033[0m,買不了該商品\n"%(salary))
60 continue
61 else:
62 print("\033[031;1m你輸入的商品不存在\033[0m")
63 for i in shopping_list:
64 pass
65 print("你已經購買了\n\033[021;1m%s\n\033[0m余額為\033[032;1m%s\033[0m"%(i, salary))
66
67 continue
68 elif number_of_product == "退出":
69 print('已購買商品列表'.center(50,'-'))
70 for i in shopping_list:
71 print(i)
72 print("余額為\033[032;1m%s\033[0m"%( salary))
73 exit()
74 else:
75 print("\033[031;1m不合法輸入,按提示重新輸入\033[0m")
76 break
77
78 else:
79 print("\033[031;1m不合法輸入,按提示重新輸入\033[0m")
80 else:
81 print("\033[031;1m不合法輸入,按提示重新輸入\033[0m")
82 exit_flag = False
83
84 elif entrance =="商家入口":
85 pass
86 else:
87 print("\033[031;1m不合法輸入,按提示重新輸入\033[0m")
?
轉載于:https://www.cnblogs.com/zzg-home/p/7066376.html
總結
- 上一篇: 导出全部记录到excel
- 下一篇: 一个老忘且非常有用的jquery动画方法