1 1.
2 '''
3 knowledge_point:
4 1.練習if...else;
5 2.練習while;
6 3.練習列表的切片取值;
7
8 requirements:
9 購物車:
10 1.程序啟動后,輸入工資,打印商品列表
11 2.提示用戶根據商品編號購買產品
12 3.選擇商品后檢測余額是否足夠,扣款或提醒余額不足
13 4.隨時退出,退出時打印已經購買的商品和余額
14
15 code:
16 '''
17 product_li = [
18 ("mac pro", 12000),
19 ("hp", 5000),
20 ("iphone", 6000),
21 ("huawei", 5000),
22 ("gopro", 4000)
23 ]
24 salary = input("input salary: ")
25 shopping_li = []
26 if salary.isdigit():
27 salary = int(salary)
28 while True:
29 for i, item in enumerate(product_li):
30 print(i+1, item)
31 choice_num = input('輸入編號購買:')
32 if choice_num.isdigit():
33 choice_num = int(choice_num)
34 if choice_num <= len(product_li) and choice_num >= 0:
35 paid_price = product_li[choice_num-1][1]
36 if paid_price <= salary:
37 paid_item = product_li[choice_num - 1]
38 print("you can pay")
39 shopping_li.append(paid_item)
40 salary -= paid_price
41 print("surplus is \033[32;1m{}\033[0m".format(salary))
42 else:
43 print('\033[41;1m not enough money {}\033[0m'.format(salary))
44 else:
45 print("product is not exist")
46 elif choice_num == "q":
47 # print("exited")
48 print('---------shopping_list_as_follows-------------')
49 for p in shopping_li:
50 print(p)
51 print("current balance is {}".format(salary))
52 exit() #退出
53 else:
54 print("invalid choice")