2019.5.16 课后练习。简易购物车程序

根据之前看过的知识点,以自己的思路先写出了一个程序,后续继续老师的操作再进行优化。

 1 salary = int(input("请输入您的工资(单位 元):"))
 2 
 3 print("以下是可购清单:")
 4 
 5 shopping_list = ['','iphone','computer','book','apple_juice','bread','cake']#商品列表
 6 commodity_prices = [0,6000,7000,2,3,3,5] #商品价格列表
 7 already_buy = []#已经购买的商品列表
 8 
 9 print("1.",shopping_list[1],commodity_prices[1],"")
10 print("2.",shopping_list[2],commodity_prices[2],"")
11 print("3.",shopping_list[3],commodity_prices[3],"")
12 print("4.",shopping_list[4],commodity_prices[4],"")
13 print("5.",shopping_list[5],commodity_prices[5],"")
14 print("6.",shopping_list[6],commodity_prices[6],"")
15 
16 select = ""
17 while select != "quit":
18     select = input("购买您需要的宝贝至购物车,请输入数字(如需结算,输入 quit):")
19     print()
20     if select == "1":
21         salary = salary - commodity_prices[1]
22         if salary < 0:
23             print("余额不足,",salary)
24             salary = salary + commodity_prices[1]
25         else:
26             print("已加入",shopping_list[1],"到你的购物车,当前余额:",salary)
27         already_buy.append(shopping_list[1])
28     elif select == "2":
29         salary = salary - commodity_prices[2]
30         if salary < 0:
31             print("余额不足,", salary)
32             salary = salary + commodity_prices[2]
33         else:
34             print("已加入", shopping_list[2], "到你的购物车,当前余额:", salary)
35         already_buy.append(shopping_list[2])
36     elif select == "3":
37         salary = salary - commodity_prices[3]
38         if salary < 0:
39             print("余额不足,", salary)
40             salary = salary + commodity_prices[3]
41         else:
42             print("已加入", shopping_list[3], "到你的购物车,当前余额:", salary)
43         already_buy.append(shopping_list[3])
44     elif select == "4":
45         salary = salary - commodity_prices[4]
46         if salary < 0:
47             print("余额不足,", salary)
48             salary = salary + commodity_prices[4]
49         else:
50             print("已加入", shopping_list[4], "到你的购物车,当前余额:", salary)
51         already_buy.append(shopping_list[4])
52     elif select == "5":
53         salary = salary - commodity_prices[5]
54         if salary < 0:
55             print("余额不足,", salary)
56             salary = salary + commodity_prices[5]
57         else:
58             print("已加入", shopping_list[5], "到你的购物车,当前余额:", salary)
59         already_buy.append(shopping_list[5])
60     elif select == "6":
61         salary = salary - commodity_prices[6]
62         if salary < 0:
63             print("余额不足,", salary)
64             salary = salary + commodity_prices[6]
65         else:
66             print("已加入", shopping_list[6], "到你的购物车,当前余额:", salary)
67         already_buy.append(shopping_list[6])
68 else:
69     print("您已购买以下商品:")
70     print(already_buy)
71     print("余额为:",salary)
72     print("谢谢惠顾,欢迎下次光临!")
View Code

 

 

2019.5.20 根据老师讲课的内容进行优化

知识点1:列表的值可以进行嵌套。 如 sale_list = [['iphone',6000],('book',500),500,'bike'] 列表里面的值可以是 列表、元组、数字、字符串等

知识点2:嵌套列表取值, sale_list[0][1] 返回值是:6000

知识点3:函数 enumerate(sale_list,1) 。 在列表“sale_list”前面自动打印序号,默认不写“1”就从“0”开始

知识点4:for sale,price,unit in sale_list:。  通过for循环,把列表中的值取出来。

知识点5:print("已购买%s,当前余额:%d" % (sale_list[select - 1][0], saving))。 定位符使用

知识点6:函数 len(product_list)。 算出列表的长度

 1 # 商品列表
 2 sale_list = [
 3     ['iphone',7000,""],
 4     ['bike',600,""],
 5     ['computer',8000,""],
 6     ['pen',3,""],
 7     ['book',2,""],
 8     ['desk',500,""]
 9 ]
10 
11 # 已购商品列表
12 shopping_list = []
13 
14 # 预存金额,让用户自己输入
15 saving = input("请输入您需要存储的金额:")
16 # 判断是否输入的为数字
17 if saving.isdigit():
18     saving = int(saving)
19 else:
20     print("请输入数字!!!")
21 
22 # 用while实现用户一直购物
23 while True:
24 
25     # for number,list in enumerate(sale_list,1):  #展示商品列表,此老师方法展示不好看,自己想了另外一个表达方式
26     #     print(number,"  ",list)
27     number = 0
28     for sale,price,unit in sale_list:
29         number += 1
30         print("商品代码:",number,"  ",sale,price,unit)
31 
32     select = input("请输入您要购买的商品代码(退出,按‘Q’):")  #让用户选择购买的商品
33     if select.isdigit():
34         select = int(select)
35         if saving >= sale_list[select - 1][1]:  #判断是否买得起
36             shopping_list.append(sale_list[select - 1]) #将购买的商品加入已购清单
37             saving -= sale_list[select - 1][1]  #算出余额
38             print("已购买%s,当前余额:%d" % (sale_list[select - 1][0], saving))
39         else:
40             print("余额不足,还剩余额为:%d" % saving)
41     elif select == "q" or select == "Q":    #结算清单
42         print("--------------您购买的清单如下-----------------")
43         for shopping,price,unit in shopping_list: #打印已购清单
44             print(shopping,price,unit)
45         print("------------------------------------------------")
46         print("welcome to you again!!!")
47         break
48     else:
49         print("输入有误,请重新正确输入")
View Code

 

转载于:https://www.cnblogs.com/kentee/p/10874785.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值