运用循环判断语句和列表的购物车程序

针对循环判读语句和列表的运用练习,对应Day2中的第一个购物车程序训练。

能力有限,可能存在不足。

 1 # Author: JC
 2 
 3 while 1:
 4     balance = input("请输入工资金额:")
 5     if balance.isdigit():
 6         balance = int (balance)
 7         buy_list = []
 8         goods_list = [[0,'Iphone',6000],[1,'Bike',800],[2,'Computer', 5800],[3,'Coffee', 49],[4,'Winds', 200]]
 9         while True :
10             for i in goods_list :
11                 print(i)
12             goods_number = int(input("请输入需要购买的商品编号:"))
13         #print(int(goods_list [int(goods_number )][-1]))
14             if goods_number < len(goods_list) and goods_number >= 0:  # 是否为列表中的数字
15                 if goods_list [goods_number ][-1] > balance :
16                     print("您的余额只有:{balance}".format(balance = balance ))
17                     print("余额不足够,请选择其他商品:")
18                     #for i in goods_list:
19                         #print(i)
20                 else:
21                     balance = balance - goods_list [goods_number][-1]
22                     buy_list_provisional = goods_list[goods_number]
23                     print("购买的商品为:{list_confim}".format(list_confim=buy_list_provisional))
24                     print("所剩余额为:%s"%balance)
25                     buy_list .append(goods_list [goods_number])
26                     buy_confim = input("是否继续购买:y/n?")
27                     if buy_confim == 'y':continue
28                     break
29             else:print("请输入存在的商品编号!")
30         print("已购买商品:")
31         #print(buy_list )
32         for i in  buy_list:
33             print(i)
34         print("用户余额为:{_balance}".format(_balance = balance ))
35         break
36     else:
37         print("请输入金额数字!")
View Code

另附,一个比较好点的购物车程序。

 1 product_list = [
 2     ('Iphone',5800),
 3     ('Mac Pro',9800),
 4     ('Bike',800),
 5     ('Watch',10600),
 6     ('Coffee',31),
 7     ('Python',120),
 8 ]
 9 shopping_list = []
10 salary = input("Input your salary:")
11 if salary.isdigit():#判断是否为数字
12     salary = int (salary)
13 
14     while True :
15         for index,item in enumerate (product_list) :#下标和列表内容一起打印
16             print(index,item)
17         user_choice = input("选择购买商品:")
18         if user_choice .isdigit():#是否是数字
19             user_choice = int (user_choice)
20             if user_choice < len(product_list ) and user_choice >= 0:#是否为列表中的数字
21                 shopping_car = product_list[user_choice ]
22                 if shopping_car [1] <= salary :#买得起
23                     shopping_list.append(shopping_car)
24                     salary -= shopping_car [1] #扣费
25                     print("Add %s into shopping car,your current balance is \033[31;1m%s\033[0m."%(shopping_car ,salary))
26                     #凸显色彩输出格式
27                 else:
28                     print("\033[23;1m你的余额只剩[%s]啦,还买个毛线啊!\033[0m"%salary )
29             else:
30                 print("Product code [%s] is not exist!"%user_choice)
31                 print("Please choice production again:")
32         elif user_choice == 'q':
33             print("--------shopping list--------")
34             for p in shopping_list :
35                 print(p)
36                 print("Your current balance:",salary )
37                 exit()
38         else:
39             print("invalid option")
40 else:
41     print("Please enter number...")
View Code

猜你喜欢

转载自www.cnblogs.com/jison0223/p/9977982.html