循环列表练习

学习循环列表的练习题:
1、列表下标的取用
2、循环和判断的逻辑

 1 goods_list=[
 2     ('huaweiP9',3200),
 3     ('Macbook',6180),
 4     ('furongwang',225),
 5     ('chengguangCup',85),
 6     ('pythonbook',84)
 7 ]
 8 shopping_list=[]
 9 salary=input("请输入你的工资:")
10 if  salary.isdigit():
11     salary=int(salary)
12     print(goods_list)
13     while True:
14         # for index,item in enumerate(goods_list):
15         for item in goods_list:
16             print(goods_list.index(item),item)
17         user_choice = input("选择要买的东西>>>:")
18         if user_choice.isdigit():
19             user_choice = int(user_choice)
20             if user_choice < len(goods_list) and user_choice >=0:
21                 p_item = goods_list[user_choice]
22                 if p_item[1] <= salary: #买的起
23                     shopping_list.append(p_item)
24                     salary -= p_item[1]
25                     print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m" %(p_item,salary) )
26                 else:
27                     print("你的余额只剩%s啦,还买个毛线" % salary)
28             else:
29                 print("你选择的商品序号 %s 不存在!"% user_choice)
30         elif user_choice == 'q':
31             print("--------购物清单------")
32             for p in shopping_list:
33                 print(p)
34             print("你的余额:",salary)
35             exit()
36         else:
37             print("输入错误,退出输入q")
View Code

 


 

posted on 2017-10-10 19:24  Alan_chow  阅读(151)  评论(0编辑  收藏  举报

导航