• 习题四 答案


    方案一:

    复制代码
     1 goods = [
     2     {"name": "电脑", "price": 1999},
     3     {"name": "鼠标", "price": 10},
     4     {"name": "游艇", "price": 20},
     5     {"name": "美女", "price": 998}
     6 ]
     7 def chongzhi():
     8     global zongzichan
     9     e = int(input("请输入充值金额:"))
    10     zongzichan = zongzichan + e
    11     print("充值成功,返回购物车.")
    12     gouwuche()
    13     
    14 def gouwuche():
    15     global zongzichan
    16     xiaofei = 0
    17     print("您选购的商品如下:")
    18     for c, c1 in enumerate(list, 1):
    19         for c2, c3 in c1.items():
    20             xiaofei = xiaofei + c3
    21             print(c, c2, c3)
    22     print("总价格:", xiaofei)
    23     while True:
    24         d = input("请选择结算(yes) or 前往充值(no):")
    25         if d == "yes" and zongzichan >= 0:
    26             print("购买成功,您一共消费了:", xiaofei, "总资产剩余:", zongzichan)
    27             print("欢迎下次光临,再见.")
    28             break
    29         elif d == "yes" and zongzichan < 0:
    30             d1 = input("购买失败,余额不足,请选择前往充值(yes) or 前往购物车移除商品(no):")
    31             if d1 == "yes":
    32                 chongzhi()
    33             elif d1 == "no":
    34                 d2 = int(input("请输入商品编号移除出购物车:"))
    35                 d3 = list[d2-1]
    36                 for d4, d5 in d3.items():
    37                     zongzichan = zongzichan + d5
    38                     del list[d2-1]
    39                     print("您移除了:", d4, d5)
    40                     gouwuche()
    41             else:
    42                 print("指令错误")
    43         elif d == "no":
    44             chongzhi()
    45         else:
    46             print("指令错误")
    47         
    48     exit()
    49     
    50 def shangpin():
    51     global list
    52     global zongzichan
    53     list = []
    54     while True:
    55         print("商品编号、名称、价格列表:")
    56         for keys, a in enumerate(goods, 1):
    57             a1 = a.get("name")
    58             a2 = a.get("price")
    59             print(keys, a1, a2)
    60         dict = {}
    61         a3 = int(input("请输入商品编号将其加入购物车:"))
    62         zongzichan = zongzichan - goods[a3-1]["price"]
    63         a4 = goods[a3-1]["name"]
    64         a5 = goods[a3-1]["price"]
    65         print("商品:",goods[a3-1]["name"], "价格:",goods[a3-1]["price"], "已加入购物车.")
    66         dict[a4] = a5
    67         list.append(dict)
    68         b = input("请选择继续购物(yes) or 去购物车结算(no):")
    69         if b == "yes":
    70             continue
    71         elif b == "no":
    72             gouwuche()
    73         else:
    74             print("指令错误")
    75                 
    76 def zichan():
    77     global zongzichan
    78     zongzichan = int(input("请输入总资产:"))
    79     shangpin()
    80     
    81 zichan()
    复制代码

    方案二(无充值):

    复制代码
     1 goods = [
     2     {"name": "电脑", "price": 1999},
     3     {"name": "鼠标", "price": 10},
     4     {"name": "游艇", "price": 20},
     5     {"name": "美女", "price": 998}
     6 ]
     7 
     8 zongzichan = int(input("请输入总资产:"))
     9 xiaofei = 0
    10 gouwuche = {}
    11 for a in goods:
    12     print(a["name"], a["price"])
    13     
    14 while True:
    15     shangpin = input("请输入商品名称加入购物车 or 输入Y/y去购物车结算:")
    16     if shangpin.lower() == "y":
    17         break
    18     for a1 in goods:
    19         if a1["name"] == shangpin:
    20             if a1["name"] in gouwuche.keys():
    21                 gouwuche[a1["name"]]["num"] = gouwuche[a1["name"]]["num"] + 1
    22             else:
    23                 gouwuche[a1["name"]] = {"num":1, "jiage":a1["price"]} 
    24 
    25 for b , b1 in gouwuche.items():
    26     b2 = b1["jiage"]
    27     b3 = b1["num"]
    28     b4 = b2 * b3
    29     xiaofei  = xiaofei + b4
    30 if xiaofei > zongzichan:
    31     print("购买失败,余额不足")
    32 else:
    33     print("购买成功")
    复制代码
  • 相关阅读:
    servlet程序开发
    jsp九大内置对象
    git原理教程
    jsp基础语法_Scriptlet_基本指令
    06_mysql多表查询
    05_mysql单表查询
    04_mysql增删改操作
    03_mysql索引的操作
    01_mysql数据库操作和基本数据类型
    生成器
  • 原文地址:https://www.cnblogs.com/xwqhl/p/10675005.html
Copyright © 2020-2023  润新知