我的第一个Python_shopping

 1 #Author:ZhouZhangzhang
 2 product_list = [
 3     ('华为P20',3800),
 4     ('荣耀手环',800),
 5     ('M6青春版平板',2900),
 6     ('Intel 760P 512G SSD',800),
 7     ('Mac Pro',8800),
 8     ('拿铁coffee',80),
 9     ('小龙虾',60),
10     ('飞机票',1199),
11 ]
12 shoping_list = []
13 while True:
14     salary = input("\033[031;1m请输入你的工资:\033[0m")
15     if salary.isdigit():
16         salary = int(salary)
17         break
18     else:
19         print("\033[031;1m请输入正确的工资金额!\033[0m")
20 while True:
21     for index,i in enumerate(product_list):
22         print(index,i)
23     user_choice = input("\033[031;1m请选择要购买商品的编号:\033[0m")
24     if user_choice.isdigit():
25         user_choice = int(user_choice)
26         if user_choice < len(product_list) and user_choice >= 0 :
27             shoping_add = product_list[user_choice]
28             if shoping_add[1] < salary:
29                 shoping_list.append(shoping_add)
30                 salary -= shoping_add[1]
31                 product = shoping_add[0]
32                 money = shoping_add[1]
33                 print("\033[031;1m恭喜,购买成功,商品【%s,%s元】已加入购物车,您当前可用余额为%s元,\n继续选购请输入商品编号,退出请输入Q\033[0m"%(product,money,salary))
34             elif shoping_add[1] == salary:
35                 shoping_list.append(shoping_add)
36                 salary -= shoping_add[1]
37                 product = shoping_add[0]
38                 money = shoping_add[1]
39                 print("\033[031;1m恭喜,购买成功,商品【%s,%s元】已加入购物车,您当前可用余额为%s元,退出请输入Q\033[0m" % (product, money, salary))
40             else:
41                 print("\033[031;1m您的余额不足请重新输入商品编号,退出请输入Q!\033[0m")
42         else:
43             print("\033[031;1m商品不存在,请重新输入0~%s商品编号\033[0m"%((len(product_list))-1))
44     elif user_choice == "q" or user_choice == "Q":
45         print("\033[031;1m----------shoping list----------\033[0m")
46         for index,j in enumerate(shoping_list):
47             print(index,j)
48         print("\033[031;1m谢谢惠顾,您的余额是%s元\033[0m"%salary)
49         exit()
50     else:
51         print("\033[031;1m商品不存在,请重新输入0~%s商品编号\033[0m"%((len(product_list))-1))
52         continue

今天开始写笔记了,这么一个简单的程序,昨天照着写了一遍,今天自己摸索了好久,老师说20分钟就能写完的代码,我tm居然写了3小时,终于摸索出来了,也更完善了,还是最最最基本的东西,上手就感觉不容易,哈哈哈

猜你喜欢

转载自www.cnblogs.com/zz8833/p/11442233.html