zoukankan      html  css  js  c++  java
  • python实现购物(小程序)

    步骤:

    1. 打印商品内容

    2. 引导用户选择商品

    3. 验证输入是否合法

    4. 将用户选择商品通过choice取出来

    5. 如果钱够,用本金saving减去该商品价格

    6. 将该商品加入购物车

    7. 循环遍历购物车里的商品,购物车存放的是已买商品

    product_list=[    ('Mac电脑',9500),    ('windows电脑',800),    ('法拉利',8800000),    ('python入门教程',100),    ('华为',6000), ]saving=input('please input your money:')shopping_car=[]if saving.isdigit():    saving=int(saving)    while True:        #打印商品内容        for i,v in enumerate(product_list,1):            print(i,'>>>>',v)          #引导用户选择商品        choice=input('选择购买商品编号[退出:q]:')         #验证输入是否合法        if choice.isdigit():            choice=int(choice)            if choice>0 and choice<=len(product_list):                #将用户选择商品通过choice取出来                p_item=product_list[choice-1]                 #如果钱够,用本金saving减去该商品价格,并将该商品加入购物车                if p_item[1]<saving:                    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('invalid input')

    运行输出结果:

    please input your money:100000001 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:1('Mac电脑', 9500)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:1('Mac电脑', 9500)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:1('Mac电脑', 9500)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:2('windows电脑', 800)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:3('法拉利', 8800000)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:3余额不足,还剩1170700('法拉利', 8800000)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:1('Mac电脑', 9500)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:4('python入门教程', 100)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:5('华为', 6000)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:6编码不存在1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:7编码不存在1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:1('Mac电脑', 9500)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:1*4invalid input1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:3余额不足,还剩1145600('法拉利', 8800000)1 >>>> ('Mac电脑', 9500)2 >>>> ('windows电脑', 800)3 >>>> ('法拉利', 8800000)4 >>>> ('python入门教程', 100)5 >>>> ('华为', 6000)选择购买商品编号[退出:q]:q------------您已经购买如下商品----------------('Mac电脑', 9500)('Mac电脑', 9500)('Mac电脑', 9500)('windows电脑', 800)('法拉利', 8800000)('Mac电脑', 9500)('python入门教程', 100)('华为', 6000)('Mac电脑', 9500)您还剩1145600元钱 Process finished with exit code 0

    欢迎关注公众号:Python爬虫数据分析挖掘,回复【开源源码】免费获取更多开源项目源码

    公众号每日更新python知识和【免费】工具

    耐得住寂寞,才能登得顶
    Gitee码云:https://gitee.com/lyc96/projects
  • 相关阅读:
    计算机相关单位换算关系的积累
    谈编程资料
    杂记toCSV
    [转载]Windows 8][Metro Style Apps]淺談Metro Style Apps的瀏覽模式及螢幕解析度
    [转载][Windows 8][Metro Style Apps]Windows 8 開發/執行環境概觀
    【转载】[Windows 8]Hello Windows 8 Windows 8 Developer Preview搶先預覽
    台湾dotnet程序员之家
    [转载]实现Application Tile 更新
    [转载]在.NET中实现OAuth身份认证
    杂记
  • 原文地址:https://www.cnblogs.com/chenlove/p/14038589.html
Copyright © 2011-2022 走看看