购物车

shopping_car = []
product_list = [
    ('Mac',9000),
    ('kindle',800),
    ('tesla',900000),
    ('python book',105),
    ('bike',2000),

]
saving = input('please input your saving:')
if saving.isdigit():  #判断saving是不是数字
   saving = int(saving) 
  while
True: for i,v in enumerate(product_list,1): #给列表上序号,括号中的1就是序号开始的数字,并进行循环 print(i,v) choice = input('选择购买商品编号[退出:q}: ')  #i v输出的结果为什么是颠倒的? if choice.isdigit(): #如果商品编号是数字 choice = int(choice) if choice > 0 and choice <= len(product_list): p_item = product_list[choice-1] if p_item[1] < saving: #p_item[1]为取列表里价格的位置 saving -= p_item[1] shopping_car.append(p_item) else: print('余额不足,还剩%s'%saving) print(p_item) else: print('编码不存在') elif choice == 'q': print('-----------您以及购买如下商品------------') for i in shopping_car: print(i) print('还剩%s元'%saving) break else: print('请输入编码')

红字为不熟,给自己小拓展,有商品数量,商品购买后叠加。

原文地址:https://www.cnblogs.com/fate2048/p/9036523.html