python复习购物车程序

个人学习总结:无他,唯手熟尔!多敲多练才是王道

Python 第三课 元组的灵活运用&字符串的诸多操作

'''
复习购物车程序
内容 
1. 启动程序后让用户输入工资然后打印商品列表

2. 允许用户根据商品编号购买商品

3. 用户选择商品后,检测余额是否充足,够则直接扣款,不够提醒

4. 可随时退出,退出时,打印已经购买的商品和余额

# 假定用户不会输错
'''
shop_list = [['哇哈哈',10],['洗衣液',20],['老白干',30]]
shop_car = []
money_left = 0
exit_shop = False

# 提示用户输入工资
salary = int(input("please input your salary:"))

while True:
     # 提示用户商品列表
     for i in range(0,len(shop_list)):
          print(i,shop_list[i][0],shop_list[i][1])
          
     # 提示用户选择商品
     num = int(input("choose the goods>>>"))
     shop_car.append(shop_list[num])

     print("the goods in car>>",shop_car)

     while not exit_shop:
          # 计算商品总额
          sum_goods = 0
          for j in shop_car[:]:
               sum_goods += j[1]
          print("the sum if the goods:",sum_goods)

          # 检查余额
          money_left = salary - sum_goods
          # 余额不足删除某件商品
          if money_left < 0:
               print("you have no enough money")
               # 打印出已经在购物车的商品
               for i in range(0,len(shop_car)):
                    print(i,shop_car[i][0],shop_car[i][1])
               clear_num = int(input("drop the goods>>"))
               shop_car.pop(clear_num)
               print(shop_car)
          # 余额充足直接扣款
          else:
               print("the money left>>>",money_left)
               exit_shop = True

     exit_shop = False

     choice = input("end press q>>>")
     if choice == 'q':
          print("------shop list------")
          for index,item in enumerate(shop_car):
               print(index,item)
          print("money left:",money_left)
          break          
               

MYK
2018年2月12日




本人计算机小白一枚,对编程有浓厚兴趣,在此贴出自己的计算机学习历程,还有很多不足,望多多指教! 读书后发现好多的内容与具体专业有偏差,没来得及完成,虽然“有时间我就会做...”是人生最大的谎言,但有时间我会继续搞定未完成的内容,有始有终,兴趣使然!
原文地址:https://www.cnblogs.com/Robin5/p/8481514.html