zoukankan      html  css  js  c++  java
  • [作业]老男孩-Python开发基础课程-购物车

    1. 商品中心:
      • 商品名称、金额
      • 加入购物车
    2. 个人中心:
      • 购物车
        • 修改购物车商品
        • 下单
      • 完成订单
        • 订单详情
      • 账户余额

    预先声明全局变量:

     1 Num_dict={'G1':'good1','G2':'good2','G3':'good3','G4':'good4',}
     2 # Num_dict 商品编号 和对应商品名
     3 Goods_dict={'good1':10,'good2':20,'good3':50,'good4':100,}
     4 # Goods_dict商品及对应单价
     5 Sc_dict={}
     6 # ShopCart 购物车
     7 Ap_dict={}
     8 # AlreadyPaid 完成的订单
     9 IdCenter_Num={'M1':'ShopCart','M2':'AlreadyPaid',}
    10 # IdCenter_Num 个人中心编号和对应功能
    11 IdCenter_dict={'ShopCart':Sc_dict,'AlreadyPaid':Ap_dict,}
    12 # IdCenter_dict 个人中心功能 对应 字典
    13 AccountBalance = [400,]
    14 # AccountBalance 账户余额

    商品选择页面:

     1 def GoodsFunction():
     2     # 打印商品编号、商品以及对应价格,以供选择
     3     print('序号   商品   价格')
     4     for i in Num_dict:
     5         print("%s	%s	%s" %(i,Num_dict.get(i),Goods_dict.get(Num_dict.get(i))))
     6     print('
    0	商品页
    1	个人中心(购物车、订单、余额)')
     7     result=input("请输入序号:").strip()
     8     # 判断传入参数
     9     if result == "":
    10         print("Error:不能为空,请重新输入!")
    11     elif result == '0'or result == '1':
    12         return result
    13     elif result in Num_dict:
    14         goodname=Num_dict.get(result)
    15         # 参数为商品编号 传入AddGood() 函数
    16         AddGood(goodname)
    17     else:
    18         print("Error:指令错误,请重新输入!")
    19     return '0'

    加入购物车

     1 def AddGood(goodname):
     2     N_1 = int(input('请输入 %s 的数量:' % goodname))
     3     if N_1 > 0:
     4         # 判断商品元素是否存在,存在则修改数量,不存在则追加
     5         if goodname in Sc_dict:
     6             Sc_dict[goodname] = N_1
     7             print(Sc_dict)
     8         else:
     9             Sc_dict.update({goodname:N_1})
    10             print(Sc_dict)
    11     else:
    12         print('Error:商品数量有误!')

    个人中心

     1 def GoIdCenter():
     2     print("欢迎来到个人中心!
    序号	选项")
     3     for i in IdCenter_Num:
     4         print("%s		%s" %(i,IdCenter_Num.get(i)))
     5     print('
    账户余额:%s
    0	商品页
    1	个人中心(购物车、订单、余额)'%AccountBalance[0])
     6     result = input("请输入序号:").strip()
     7     # 判断传入参数,对应执行
     8     if result == "":
     9         print("Error:不能为空,请重新输入!")
    10     elif result == '0'or result == '1':
    11         return result
    12     elif result in IdCenter_Num:
    13         if result == 'M1':
    14             # 购物车操作函数 flag作为标识,如果返回不为空则将返回值再次返回并跳出循环
    15             flag=''
    16             while True:
    17                 flag=ShopCartFun()
    18                 if flag != '':
    19                     return flag
    20                     continue
    21         elif result == 'M2':
    22             # 订单查看函数
    23             GoodOrder()
    24             return '1'
    25     else:
    26         print("Error:指令错误,请重新输入!")
    27     return '1'

    购物车操作

     1 def ShopCartFun():
     2     # 判断购物车是否为空
     3     if Sc_dict:
     4         print('购物车
    商品名	数量	单价	总价')
     5         for i in Sc_dict:
     6             # 打印购物车商品详单
     7             print('%s	%s		%s		%s'%(i,Sc_dict.get(i),Goods_dict.get(i),Sc_dict.get(i) * Goods_dict.get(i)))
     8     else:
     9         print('你的购物车空空如也!')
    10     print('
    0	商品页
    1	个人中心(购物车、订单、余额)
    2	清空购物车
    3	结账
    修改商品数量请输入商品名。')
    11     result = input('请输入:').strip()
    12     if result == '':
    13         print("Error:不能为空,请重新输入!")
    14     elif result == '0' or result == '1' or result == '2' or result == '3':
    15         # 3 为结账选项,返回到主函数,调用结账函数
    16         return result
    17     elif result in Sc_dict:
    18         # 修改商品数量,再次调用AddGood()函数
    19         AddGood(result)
    20     else:
    21         print("Error:指令错误,请重新输入!")
    22     return ''
    结账函数
     1 def CheckOut():
     2     # Total 购物车金额合计
     3     Total = 0
     4     for i in Sc_dict:
     5         Total += Sc_dict.get(i) * Goods_dict.get(i)
     6     print(Total)
     7     # 判断是否满足结账条件
     8     if Total > AccountBalance[0]:
     9         print('余额不足!')
    10     else:
    11         import time
    12         # 生成时间戳,作为订单号
    13         Date = int(time.time())
    14         # 生成订单
    15         Ap_dict.update({Date:Sc_dict})
    16         # 扣款
    17         AccountBalance[0] = AccountBalance[0] - Total
    18         # 购物车字典在主程序清空,函数内清空似乎有些问题
    19     return '1'

    查看订单详情

     1 def GoodOrder():
     2     if Ap_dict:
     3         print('完成订单
    订单号')
     4         for i in Ap_dict:
     5             print(i)
     6         print('
    0	商品页
    1	个人中心(购物车、订单、余额)')
     7         N_2 = int(input('
    输入要查看的订单号:').strip())
     8         if N_2 == '':
     9             print("Error:不能为空,请重新输入!")
    10         elif N_2 == '0' or N_2 == '1':
    11             return N_2
    12         elif N_2 in Ap_dict:
    13             print('订单%s详情商品名	数量	总价'%N_2)
    14             for i in Ap_dict.get(N_2):
    15                 print('%s	%s	%s'%(i,Ap_dict.get(N_2).get(i),Goods_dict.get(i) * Ap_dict.get(N_2).get(i)))
    16         else:
    17             print("Error:输入有误!")

    主函数

     1 N = GoodsFunction()
     2 while True:
     3     if N == '0':
     4         N = GoodsFunction()
     5     elif N == '1':
     6         N = GoIdCenter()
     7     elif N == '2':
     8         Sc_dict= {}
     9         N = GoodsFunction()
    10     elif N == '3':
    11         N = CheckOut()

    完整代码:

      1 Num_dict={'G1':'good1','G2':'good2','G3':'good3','G4':'good4',}
      2 # Num_dict 商品编号 和对应商品名
      3 Goods_dict={'good1':10,'good2':20,'good3':50,'good4':100,}
      4 # Goods_dict商品及对应单价
      5 Sc_dict={}
      6 # ShopCart 购物车
      7 Ap_dict={}
      8 # AlreadyPaid 完成的订单
      9 IdCenter_Num={'M1':'ShopCart','M2':'AlreadyPaid',}
     10 # IdCenter_Num 个人中心编号和对应功能
     11 IdCenter_dict={'ShopCart':Sc_dict,'AlreadyPaid':Ap_dict,}
     12 # IdCenter_dict 个人中心功能 对应 字典
     13 AccountBalance = [400,]
     14 # AccountBalance 账户余额
     15 
     16 
     17 ## 商品选择页面
     18 def GoodsFunction():
     19     # 打印商品编号、商品以及对应价格,以供选择
     20     print('序号   商品   价格')
     21     for i in Num_dict:
     22         print("%s	%s	%s" %(i,Num_dict.get(i),Goods_dict.get(Num_dict.get(i))))
     23     print('
    0	商品页
    1	个人中心(购物车、订单、余额)')
     24     result=input("请输入序号:").strip()
     25     # 判断传入参数
     26     if result == "":
     27         print("Error:不能为空,请重新输入!")
     28     elif result == '0'or result == '1':
     29         return result
     30     elif result in Num_dict:
     31         goodname=Num_dict.get(result)
     32         # 参数为商品编号 传入AddGood() 函数
     33         AddGood(goodname)
     34     else:
     35         print("Error:指令错误,请重新输入!")
     36     return '0'
     37 
     38 
     39 ## 商品加入购物车
     40 def AddGood(goodname):
     41     N_1 = int(input('请输入 %s 的数量:' % goodname))
     42     if N_1 > 0:
     43         # 判断商品元素是否存在,存在则修改数量,不存在则追加
     44         if goodname in Sc_dict:
     45             Sc_dict[goodname] = N_1
     46             print(Sc_dict)
     47         else:
     48             Sc_dict.update({goodname:N_1})
     49             print(Sc_dict)
     50     else:
     51         print('Error:商品数量有误!')
     52 
     53 ## 个人中心
     54 def GoIdCenter():
     55     print("欢迎来到个人中心!
    序号	选项")
     56     for i in IdCenter_Num:
     57         print("%s		%s" %(i,IdCenter_Num.get(i)))
     58     print('
    账户余额:%s
    0	商品页
    1	个人中心(购物车、订单、余额)'%AccountBalance[0])
     59     result = input("请输入序号:").strip()
     60     # 判断传入参数,对应执行
     61     if result == "":
     62         print("Error:不能为空,请重新输入!")
     63     elif result == '0'or result == '1':
     64         return result
     65     elif result in IdCenter_Num:
     66         if result == 'M1':
     67             # 购物车操作 flag作为标识,如果返回不为空则将返回值再次返回并跳出循环
     68             flag=''
     69             while True:
     70                 flag=ShopCartFun()
     71                 if flag != '':
     72                     return flag
     73                     continue
     74         elif result == 'M2':
     75             # 订单查看
     76             GoodOrder()
     77             return '1'
     78     else:
     79         print("Error:指令错误,请重新输入!")
     80     return '1'
     81 
     82 
     83 def ShopCartFun():
     84     # 判断购物车是否为空
     85     if Sc_dict:
     86         print('购物车
    商品名	数量	单价	总价')
     87         for i in Sc_dict:
     88             # 打印购物车商品详单
     89             print('%s	%s		%s		%s'%(i,Sc_dict.get(i),Goods_dict.get(i),Sc_dict.get(i) * Goods_dict.get(i)))
     90     else:
     91         print('你的购物车空空如也!')
     92     print('
    0	商品页
    1	个人中心(购物车、订单、余额)
    2	清空购物车
    3	结账
    修改商品数量请输入商品名。')
     93     result = input('请输入:').strip()
     94     if result == '':
     95         print("Error:不能为空,请重新输入!")
     96     elif result == '0' or result == '1' or result == '2' or result == '3':
     97         # 3 为结账选项,返回到主函数,调用结账函数
     98         return result
     99     elif result in Sc_dict:
    100         # 修改商品数量,再次调用AddGood()函数
    101         AddGood(result)
    102     else:
    103         print("Error:指令错误,请重新输入!")
    104     return ''
    105 
    106 
    107 #Ap_dict={}   AccountBalance=300   Sc_dict={}
    108 ## 结账
    109 def CheckOut():
    110     # Total 购物车金额合计
    111     Total = 0
    112     for i in Sc_dict:
    113         Total += Sc_dict.get(i) * Goods_dict.get(i)
    114     print(Total)
    115     # 判断是否满足结账条件
    116     if Total > AccountBalance[0]:
    117         print('余额不足!')
    118     else:
    119         import time
    120         # 生成时间戳,作为订单号
    121         Date = int(time.time())
    122         # 生成订单
    123         Ap_dict.update({Date:Sc_dict})
    124         # 扣款
    125         AccountBalance[0] = AccountBalance[0] - Total
    126         # 购物车字典在主程序清空,函数内清空似乎有些问题
    127     return '1'
    128 
    129 
    130 def GoodOrder():
    131     if Ap_dict:
    132         print('完成订单
    订单号')
    133         for i in Ap_dict:
    134             print(i)
    135         print('
    0	商品页
    1	个人中心(购物车、订单、余额)')
    136         N_2 = int(input('
    输入要查看的订单号:').strip())
    137         if N_2 == '':
    138             print("Error:不能为空,请重新输入!")
    139         elif N_2 == '0' or N_2 == '1':
    140             return N_2
    141         elif N_2 in Ap_dict:
    142             print('订单%s详情商品名	数量	总价'%N_2)
    143             for i in Ap_dict.get(N_2):
    144                 print('%s	%s	%s'%(i,Ap_dict.get(N_2).get(i),Goods_dict.get(i) * Ap_dict.get(N_2).get(i)))
    145         else:
    146             print("Error:输入有误!")
    147 
    148 
    149 
    150 N = GoodsFunction()
    151 while True:
    152     if N == '0':
    153         N = GoodsFunction()
    154     elif N == '1':
    155         N = GoIdCenter()
    156     elif N == '2':
    157         Sc_dict= {}
    158         N = GoodsFunction()
    159     elif N == '3':
    160         N = CheckOut()
    View Code
  • 相关阅读:
    MySQL性能调优my.cnf详解
    Linux学习之CentOS(二十八)--RAID原理基础及Linux下软件RAID配置
    Linux学习之CentOS(二十)--CentOS6.4下修改MySQL编码方法
    Linux学习之CentOS(二十一)--Linux系统启动详解
    Linux学习之CentOS(二十二)--单用户模式下修改Root用户的密码
    Linux下oracle数据库启动和关闭操作
    编程之美——判断一个数是否为2的幂
    编程之美——求两个整数不同位数的个数
    LeetCode——Increasing Triplet Subsequence
    LeetCode——Find Minimum in Rotated Sorted Array II
  • 原文地址:https://www.cnblogs.com/ikmi/p/6195065.html
Copyright © 2011-2022 走看看