learn_1 购买商品练习

购买商品的小测试

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 #购买商品练习
 4 
 5 proudect_list = [
 6     ("iphone 11", 4999),
 7     ("huawei mate30", 3999),
 8     ("ipad mini5", 2500),
 9     ("k8s book", 198),
10     ("coffer", 20),
11     ("bike", 600),
12 ]
13 shopping_list = []
14 user_money = input("请输入你的钱数:")
15 
16 #isdigit 判断int为真,则进入下一步
17 if user_money.isdigit():
18     user_money = int(user_money)
19     while True:
20         #enumerate 方法 可以给列表添加序号
21         for index, item in enumerate(proudect_list):
22             print(index+1, item)
23         user_choice = input("请输入需要购买商品的编号,输入q退出 >>>:")
24         if user_choice.isdigit():
25             user_choice = int(user_choice)
26             if user_choice > 0 and user_choice <= len(proudect_list):
27                 user_choice = user_choice-1
28                 p_item = proudect_list[user_choice]
29                 if int(user_money) >= int(p_item[1]):
30                     shopping_list.append(p_item)
31                     user_money -= p_item[1]
32                     print("购买的商品 %s , 剩余钱数 %s" % (shopping_list, user_money))
33                 else:
34                     print("\033[41:1m你的钱不够,2333\033[0m")
35             else:
36                 print("输入编号不对,商品不存在")
37 
38         elif user_choice == 'q':
39             print("已经购买的商品 %s , 剩余钱数 %s" % (shopping_list, user_money))
40             exit()
41         else:
42             print("invaild option!!")

 

posted on 2019-10-10 16:53  voua  阅读(105)  评论(0编辑  收藏  举报

导航