购物车练习
现有知识编写的代码,还有很多地方需要优化。先记录一下!
salary = int(input("salary is :")) #首先输入工资卡资金
msc = '''
---------------shopping list-------------
1. iphone12 6800
2. mac book 9000
3. bubble 18
4. python book 60
5. bicyle 700
-----------------end---------------------
'''
print(msc) #打印商品清单
shopping_list = ["1",'iphone12s',6800,"2","mac book",9000,"3","coffee",32,"4","python book",80,"5","bicyle",1500]
#number = input(">>>") #选择商品
#print(shopping_list.index(number))
count = 0 #计算购买商品数量
LIST = [] #保存购买的商品和金额
while True:
number = input(">>>:") #选择商品
if number == 'quit': #当用户输入quit退出循环,结束购物
break
goods = shopping_list[shopping_list.index(number)+1] #通过索引寻找对应列表中的商品
price = shopping_list[shopping_list.index(number)+2] #通过索引寻找对应列表中的商品的金额
if salary < price: #当商品金额大于收入提示余额不足
print("余额不足, "+'-'+str(price-salary))
else:
print("已加入"+goods+"到你的购物车,"+"当前余额:"+str(salary-price)) #打印选购的商品
salary = salary-price #计算购买商品后余额
LIST.append(goods+' '+str(price)) #将购买商品和金额列入已购列表
count += 1 #累计购买商品的数量
continue
print("您已购买以下商品")
for i in range(count): #打印已购买商品信息
print(LIST[i])
else:
print("您的余额为:"+str(salary))
print("欢迎下次光临" )