python-利用python写一个购物小程序

一、shopping思路


  1. 打印商品内容
  2. 引导用户选择商品
  3. 验证输入是否合法
  4. 将用户选择商品通过choice取出来
  5. 如果钱够,用本金saving减去该商品价格
  6. 将该商品加入购物车
  7. 循环遍历购物车里的商品,购物车存放的是已买商品
  1.  
    product_list=[
  2.  
    ('Mac电脑',9500),
  3.  
    ('windows电脑',800),
  4.  
    ('法拉利',8800000),
  5.  
    ('python入门教程',100),
  6.  
    ('华为',6000),
  7.  
     
  8.  
    ]
  9.  
    saving=input('please input your money:')
  10.  
    shopping_car=[]
  11.  
    if saving.isdigit():
  12.  
    saving=int(saving)
  13.  
    while True:
  14.  
    #打印商品内容
  15.  
    for i,v in enumerate(product_list,1):
  16.  
    print(i,'>>>>',v)
  17.  
     
  18.  
    #引导用户选择商品
  19.  
    choice=input('选择购买商品编号[退出:q]:')
  20.  
     
  21.  
    #验证输入是否合法
  22.  
    if choice.isdigit():
  23.  
    choice=int(choice)
  24.  
    if choice>0 and choice<=len(product_list):
  25.  
    #将用户选择商品通过choice取出来
  26.  
    p_item=product_list[choice-1]
  27.  
     
  28.  
    #如果钱够,用本金saving减去该商品价格,并将该商品加入购物车
  29.  
    if p_item[1]<saving:
  30.  
    saving-=p_item[1]
  31.  
     
  32.  
    shopping_car.append(p_item)
  33.  
     
  34.  
    else:
  35.  
    print('余额不足,还剩%s'%saving)
  36.  
    print(p_item)
  37.  
    else:
  38.  
    print('编码不存在')
  39.  
    elif choice=='q':
  40.  
    print('------------您已经购买如下商品----------------')
  41.  
    #循环遍历购物车里的商品,购物车存放的是已买商品
  42.  
    for i in shopping_car:
  43.  
    print(i)
  44.  
    print('您还剩%s元钱'%saving)
  45.  
    break
  46.  
    else:
  47.  
    print('invalid input')

运行输出结果:

  1.  
    please input your money:10000000
  2.  
    1 >>>> ('Mac电脑', 9500)
  3.  
    2 >>>> ('windows电脑', 800)
  4.  
    3 >>>> ('法拉利', 8800000)
  5.  
    4 >>>> ('python入门教程', 100)
  6.  
    5 >>>> ('华为', 6000)
  7.  
    选择购买商品编号[退出:q]:1
  8.  
    ('Mac电脑', 9500)
  9.  
    1 >>>> ('Mac电脑', 9500)
  10.  
    2 >>>> ('windows电脑', 800)
  11.  
    3 >>>> ('法拉利', 8800000)
  12.  
    4 >>>> ('python入门教程', 100)
  13.  
    5 >>>> ('华为', 6000)
  14.  
    选择购买商品编号[退出:q]:1
  15.  
    ('Mac电脑', 9500)
  16.  
    1 >>>> ('Mac电脑', 9500)
  17.  
    2 >>>> ('windows电脑', 800)
  18.  
    3 >>>> ('法拉利', 8800000)
  19.  
    4 >>>> ('python入门教程', 100)
  20.  
    5 >>>> ('华为', 6000)
  21.  
    选择购买商品编号[退出:q]:1
  22.  
    ('Mac电脑', 9500)
  23.  
    1 >>>> ('Mac电脑', 9500)
  24.  
    2 >>>> ('windows电脑', 800)
  25.  
    3 >>>> ('法拉利', 8800000)
  26.  
    4 >>>> ('python入门教程', 100)
  27.  
    5 >>>> ('华为', 6000)
  28.  
    选择购买商品编号[退出:q]:2
  29.  
    ('windows电脑', 800)
  30.  
    1 >>>> ('Mac电脑', 9500)
  31.  
    2 >>>> ('windows电脑', 800)
  32.  
    3 >>>> ('法拉利', 8800000)
  33.  
    4 >>>> ('python入门教程', 100)
  34.  
    5 >>>> ('华为', 6000)
  35.  
    选择购买商品编号[退出:q]:3
  36.  
    ('法拉利', 8800000)
  37.  
    1 >>>> ('Mac电脑', 9500)
  38.  
    2 >>>> ('windows电脑', 800)
  39.  
    3 >>>> ('法拉利', 8800000)
  40.  
    4 >>>> ('python入门教程', 100)
  41.  
    5 >>>> ('华为', 6000)
  42.  
    选择购买商品编号[退出:q]:3
  43.  
    余额不足,还剩1170700
  44.  
    ('法拉利', 8800000)
  45.  
    1 >>>> ('Mac电脑', 9500)
  46.  
    2 >>>> ('windows电脑', 800)
  47.  
    3 >>>> ('法拉利', 8800000)
  48.  
    4 >>>> ('python入门教程', 100)
  49.  
    5 >>>> ('华为', 6000)
  50.  
    选择购买商品编号[退出:q]:1
  51.  
    ('Mac电脑', 9500)
  52.  
    1 >>>> ('Mac电脑', 9500)
  53.  
    2 >>>> ('windows电脑', 800)
  54.  
    3 >>>> ('法拉利', 8800000)
  55.  
    4 >>>> ('python入门教程', 100)
  56.  
    5 >>>> ('华为', 6000)
  57.  
    选择购买商品编号[退出:q]:4
  58.  
    ('python入门教程', 100)
  59.  
    1 >>>> ('Mac电脑', 9500)
  60.  
    2 >>>> ('windows电脑', 800)
  61.  
    3 >>>> ('法拉利', 8800000)
  62.  
    4 >>>> ('python入门教程', 100)
  63.  
    5 >>>> ('华为', 6000)
  64.  
    选择购买商品编号[退出:q]:5
  65.  
    ('华为', 6000)
  66.  
    1 >>>> ('Mac电脑', 9500)
  67.  
    2 >>>> ('windows电脑', 800)
  68.  
    3 >>>> ('法拉利', 8800000)
  69.  
    4 >>>> ('python入门教程', 100)
  70.  
    5 >>>> ('华为', 6000)
  71.  
    选择购买商品编号[退出:q]:6
  72.  
    编码不存在
  73.  
    1 >>>> ('Mac电脑', 9500)
  74.  
    2 >>>> ('windows电脑', 800)
  75.  
    3 >>>> ('法拉利', 8800000)
  76.  
    4 >>>> ('python入门教程', 100)
  77.  
    5 >>>> ('华为', 6000)
  78.  
    选择购买商品编号[退出:q]:7
  79.  
    编码不存在
  80.  
    1 >>>> ('Mac电脑', 9500)
  81.  
    2 >>>> ('windows电脑', 800)
  82.  
    3 >>>> ('法拉利', 8800000)
  83.  
    4 >>>> ('python入门教程', 100)
  84.  
    5 >>>> ('华为', 6000)
  85.  
    选择购买商品编号[退出:q]:1
  86.  
    ('Mac电脑', 9500)
  87.  
    1 >>>> ('Mac电脑', 9500)
  88.  
    2 >>>> ('windows电脑', 800)
  89.  
    3 >>>> ('法拉利', 8800000)
  90.  
    4 >>>> ('python入门教程', 100)
  91.  
    5 >>>> ('华为', 6000)
  92.  
    选择购买商品编号[退出:q]:1*4
  93.  
    invalid input
  94.  
    1 >>>> ('Mac电脑', 9500)
  95.  
    2 >>>> ('windows电脑', 800)
  96.  
    3 >>>> ('法拉利', 8800000)
  97.  
    4 >>>> ('python入门教程', 100)
  98.  
    5 >>>> ('华为', 6000)
  99.  
    选择购买商品编号[退出:q]:3
  100.  
    余额不足,还剩1145600
  101.  
    ('法拉利', 8800000)
  102.  
    1 >>>> ('Mac电脑', 9500)
  103.  
    2 >>>> ('windows电脑', 800)
  104.  
    3 >>>> ('法拉利', 8800000)
  105.  
    4 >>>> ('python入门教程', 100)
  106.  
    5 >>>> ('华为', 6000)
  107.  
    选择购买商品编号[退出:q]:q
  108.  
    ------------您已经购买如下商品----------------
  109.  
    ('Mac电脑', 9500)
  110.  
    ('Mac电脑', 9500)
  111.  
    ('Mac电脑', 9500)
  112.  
    ('windows电脑', 800)
  113.  
    ('法拉利', 8800000)
  114.  
    ('Mac电脑', 9500)
  115.  
    ('python入门教程', 100)
  116.  
    ('华为', 6000)
  117.  
    ('Mac电脑', 9500)
  118.  
    您还剩1145600元钱
  119.  
     
  120.  
    Process finished with exit code 0
     
      

    本文首发于python黑洞网,博客园同步更新

原文地址:https://www.cnblogs.com/pythonzhilian/p/13690059.html