zoukankan      html  css  js  c++  java
  • 利用列表的知识写一个购物小程序

    利用列表,写一个购物小程序,实现显示商品,显示余额,显示购物车的商品,可以随时退出。

     1 product_list = [
     2     ('Mac', 9000),
     3     ('Kindle', 800),
     4     ('tesla', 900000),
     5     ('python book', 105),
     6     ('bike', 2000),
     7 ]
     8 
     9 saving = input('please input your money:')
    10 shopping_car = []
    11 if saving.isdigit():
    12     saving = int(saving)
    13 
    14     #   for i in product_list:
    15     #      print(product_list.index(i),i)
    16     while True:
    17         # 打印商品内容
    18         for i, v in enumerate(product_list, 1):  # 逗号后面的数字是表示从多少开始索引
    19             print(i, '>>>', v)
    20 
    21         # 引导用户选择商品
    22         choice = input('选择购买商品编号【退出: q】: ')
    23 
    24         # 验证输入是否合法
    25         if choice.isdigit():
    26             choice = int(choice)
    27             if choice > 0 and choice <= len(product_list):
    28                 # 将用户选择的商品通过choice取出来
    29                 p_item = product_list[choice - 1]
    30 
    31                 # 如果钱够 ,用本金saving减去商品价格,并将商品加入购物车
    32                 if p_item[1] < saving:
    33                     saving -= p_item[1]
    34                     shopping_car.append(p_item)
    35                     print('***您已经将%s加入购物车***' % p_item[0])
    36                     for i in shopping_car:
    37                         print(i)
    38                     print('您还剩%s元钱' % saving)
    39                 else:
    40                     print('余额不足,还剩%s' % saving)
    41                 print(p_item)
    42             else:
    43                 print("编码不存在")
    44         elif choice == 'q':
    45             print('---------您已经购买如下商品---------')
    46             # 循环遍历购物车里面的商品,购物车存放的是已经买的商品
    47             for i in shopping_car:
    48                 print(i)
    49             print('您还剩%s元钱' % saving)
    50             break
    51         else:
    52             print('invalid input!!')
  • 相关阅读:
    使用 Spring + CXF 发布 REST 服务
    在web项目中使用CXF暴露REST服务
    学习SpringMVC——从HelloWorld开始
    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
    安装maven时JAVA_HOME的配置小问题
    java 接口的实现和继承关系
    servlet的两种配置方式
    C# MainWindowHandle为0的解决方法
    C# WindowsMediaPlayer 的一些用法
    C# 获取MP3信息
  • 原文地址:https://www.cnblogs.com/wyh-study/p/11146144.html
Copyright © 2011-2022 走看看