• python编写购物车


    上次的学习又没有坚持下来,工作忙的不可开交,但我反思了一下还是自己没有下定决心好好学习,所以这次为期3个月的学习计划开始了,下面是这次学习后重新编写的购物车初版代码。

     1 # 功能要求:
     2 # 要求用户输入总资产,例如:2000
     3 # 显示商品列表,让用户选择商品,加入购物车
     4 # 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。
     5 # 附加:可充值、某商品移除购物车
     6 goods = [
     7     {"name": "电脑", "price": 1999},
     8     {"name": "鼠标", "price": 10},
     9     {"name": "游艇", "price": 20},
    10     {"name": "美女", "price": 998},
    11 ]
    12 zongxiaofei = 0
    13 gouwuche = {}
    14 zongzichan = int(input("欢迎来到小小商店,请输入您的总资产:"))
    15 print("您的总资产为:", zongzichan, "
    商品名称、价格如下:")
    16 for shangpin_list in goods:
    17     print(shangpin_list["name"], shangpin_list["price"])
    18 print("
    ")
    19 while True:
    20     goumai = input("请输入'商品名称'加入购物车或者输入'y'或'Y'去购物车进行结算:")
    21     if goumai.lower() == "y":
    22         break
    23     for shangpin_list2 in goods:
    24         if shangpin_list2["name"] == goumai:
    25             if  shangpin_list2["name"] not in gouwuche.keys():
    26                 gouwuche[shangpin_list2["name"]] = {"num": 1, "jiage": shangpin_list2["price"]}
    27             else:
    28                 gouwuche[shangpin_list2["name"]]["num"] = gouwuche[shangpin_list2["name"]]["num"] + 1
    29 for jiesuan_key , jiesuan_values in gouwuche.items():
    30     jiesuan_num = jiesuan_values["num"]
    31     jiesuan_jiage = jiesuan_values["jiage"]
    32     xiaofei = jiesuan_num * jiesuan_jiage
    33     zongxiaofei = zongxiaofei + xiaofei
    34 while True:
    35     if zongxiaofei > zongzichan:
    36         print("购买失败!")
    37         print("您的总资产为:", zongzichan, "您总消费金额:", zongxiaofei, "超出金额:", zongxiaofei - zongzichan)
    38         chongzhi = input("请输入'y'or'Y'前往充值或输入'n'or'N'回到购物车删除多余商品:")
    39         while True:
    40             if chongzhi.lower() == "y":
    41                 chongzhi_money = int(input("请输入充值金额:"))
    42                 zongzichan = zongzichan + chongzhi_money
    43                 print("充值成功,您的总资产为:", zongzichan, "
    ")
    44                 break
    45             if chongzhi.lower() == "n":
    46                 print("
    您购物车内消费商品名称、商品数量、商品单价如下:")
    47                 for jiesuan_key2, jiesuan_values2 in gouwuche.items():
    48                     print(jiesuan_key2, jiesuan_values2["num"], jiesuan_values2["jiage"])
    49                 shanchu = input("
    请输入商品名称来减少商品数量1:")
    50                 for jiesuan_key3 , jiesuan_values3 in gouwuche.items():
    51                     if jiesuan_key3 == shanchu:
    52                         if jiesuan_key3 in gouwuche.keys():
    53                             zongxiaofei = zongxiaofei - gouwuche[jiesuan_key3]["jiage"]
    54                 break
    55     else:
    56         print("购买成功!")
    57         print("您的总资产为:", zongzichan, "您总消费金额:", zongxiaofei, "结余金额:", zongzichan - zongxiaofei)
    58         exit()

    这个版本是带充值、删除购物车商品,最后结算金额。

    后面我会学习简化代码,编写另一版实时判断余额的。

  • 相关阅读:
    C#正则表达式判断输入日期格式是否正确
    Linq 总结
    sql存储过程
    uploadify多文件上传实例--C#
    Get W3WP List when Debugging
    SharePoint 2010 BI:Chart Web Part
    Versioning SharePoint 2010 Workflow In VS
    Multilingual User Interface (MUI) In SharePoint 2013
    Create Custom Modification Form In VS 2012-Part1
    Create Custom Modification Form In VS 2012-Part2
  • 原文地址:https://www.cnblogs.com/zhangzengqiang/p/11159835.html
Copyright © 2020-2023  润新知