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日

 

 

 

 

 

 




 

posted @ 2018-02-27 23:19  小白的个人总结  阅读(224)  评论(0编辑  收藏  举报