zoukankan      html  css  js  c++  java
  • 环境小硕的转行之路-6-第二次作业:有管理员模式的购物车

    前几天由于过敏躺尸了两周,感觉都快废了。想要再次跨考计算机的计划遭到了家人的激烈反对。最后不了了之,对本行业越来越失望,希望通过这三年的自学成功跳行。

    这次作业多学了好多东西才做出来深感码力不足,if-else循环用的太多了。即便刚学了函数,还是不知道使用函数精简代码的套路。希望后续课程有作补充。


    作业要求

    一、购物模式:
    1.顾客输入薪水后打印商品列表
    2.顾客可以根据商品列表的下标购买商品
    3.若薪水不足支付购买的商品会报错
    4.顾客退出的时候打印购买的商品列表并显示余额退出
    二、管理员模式
    1.在输入薪水的时候输入:administrator即可进入管理员模式
    2.可根据商品的编号修改商品名称、价格
    3.可添加新的商品
    4.最后把修改后的商品数据写入商品文本中


    前期准备工作

    在源码同一文件夹下创建一商品文本内容如下: 

    源代码

     1 #author : negu
     2 # -*- coding: utf-8 -*-
     3 def list_loop(list):#展示商品列表的函数
     4     for index,i in enumerate(list):#把每项的下标取出来
     5         if index == 0:
     6             continue
     7         print(index,i)
     8 view_list = ['blank']#商品列表,第一项占位,让商品序号从1开始。
     9 with open('goods','r+',encoding='utf-8') as f:#如上一次作业一样,把文件导入列表
    10     for line in f:
    11         goodname = line.strip().split(':')[0]
    12         goodprice = line.strip().split(':')[1]
    13         view_list.append([goodname,goodprice])#以子列表形式导入列表
    14 salary = input("please input your salary:	")
    15 if salary == 'administrator':#进入管理员模式
    16     while True:
    17         list_loop(view_list)
    18         choose = input("1.change the item name,2.chage the item price,3.add new item,else:quit:	")
    19         if choose == '1':
    20             choose_1 = input("which item's name do you want to chage?:	")#想改名字的商品的编号
    21             if choose_1.isdigit():
    22                 choose_1 = int(choose_1)
    23                 if choose_1 <= len(view_list) and choose_1 >= 0 :#判断是否在列表中,重复代码下次多次出现
    24                     p_1 = view_list[choose_1]
    25                     choose_1_1 = input('input its new name:	')#新名字
    26                     p_1[0] = choose_1_1
    27                 else :
    28                     print('invalid number')
    29                     continue
    30             else :
    31                 print('invalid number')
    32                 continue
    33         elif choose == '2':
    34             choose_2 = input("which item's price do you want to change?:	")#想改价格的商品的编号
    35             if choose_2.isdigit():
    36                 choose_2 = int(choose_2)
    37                 if choose_2 <= len(view_list) and choose_2 >= 0 :
    38                     p_1 = view_list[choose_2]
    39                     choose_2_2 = input('input its new price:	')#新价格
    40                 else:
    41                     print('invalid number')
    42                     continue
    43             else:
    44                  print('invalid number')
    45                  continue
    46             p_1[1] = choose_2_2
    47         elif choose == '3':#添加新商品
    48             temporary_name = input("input your good's name:	")
    49             temporary_price = input("input your good's price:	")
    50             if temporary_price.isdigit():
    51                 pass
    52             else:
    53                 print('invalid number')
    54                 continue
    55             view_list.append([temporary_name,temporary_price])#临时列表追加新商品
    56         elif choose == 'q':
    57             with open('goods','w+',encoding='utf-8') as fi :
    58                 for i in view_list:
    59                         if i == 'blank':
    60                             continue
    61                         s = '%s:%s
    '%(i[0],i[1])
    62                         fi.write(s)#将临时列表的商品写入文本中
    63             exit()
    64         else :
    65             print('invalid number')
    66             continue
    67 elif salary.isdigit():
    68     salary = int(salary)
    69 elif not salary.isdigit():
    70     print('invalid input')
    71     exit()
    72 cart = []
    73 while True :
    74     for index,i in enumerate(view_list):#把每项的下标取出来
    75         if index == 0:
    76             continue
    77         print(index,i)
    78     serial_number = input(" please input item number:	")
    79     if serial_number.isdigit():
    80         serial_number = int(serial_number)
    81         if serial_number <= len(view_list) and serial_number >= 0 :
    82             p_i = view_list[serial_number]
    83             if p_i[1].isdigit():
    84                 p_i[1] = int(p_i[1])
    85             if p_i[1] <= salary: #买得起
    86                 cart.append(p_i[0])
    87                 salary -= p_i[1]
    88                 print("added %s into shopping cart,your current balance is %s"%(p_i,salary))
    89             else : print("33[41;1myour current balance is not enough.33[0m")
    90          #len函数返回的是列表的长度
    91         else:
    92             print("商品不存在")
    93     elif serial_number == 'q' :
    94         print("您已购买的产品有:",cart)
    95         exit()
    96     else:
    97         print("invalid input")
    98         continue
    源码

    测试方法

    一、管理员模式 1.进入管理员模式将banana改成watermelon,价格改成25.
    2.加一个新的商品pear,价格8
    二、顾客模式
    1.输入2000薪水随便买些东西
    2.输入20薪水买爆报错
    3.输入'fuck off'看一下系统的识别能力

    结果

    一、

    1.输出结果:

    please input your salary:	administrator
    1 ['banana', '10']
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    1.change the item name,2.chage the item price,3.add new item,else:quit:	1
    which item's name do you want to chage?:	1
    input its new name:	watermelon
    1 ['watermelon', '10']
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    1.change the item name,2.chage the item price,3.add new item,else:quit:	2
    which item's price do you want to change?:	1
    input its new price:	25
    1 ['watermelon', '25']
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    1.change the item name,2.chage the item price,3.add new item,else:quit:	3
    input your good's name:	pear
    input your good's price:	8
    1 ['watermelon', '25']
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    5 ['pear', '8']
    1.change the item name,2.chage the item price,3.add new item,else:quit:	q
    
    Process finished with exit code 0
    

      

    2.商品文本图:

     

    二、

    1.正常买东西输出结果:

    please input your salary:	2000
    1 ['watermelon', '25']
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    5 ['pear', '8']
     please input item number:	1
    added ['watermelon', 25] into shopping cart,your current balance is 1975
    1 ['watermelon', 25]
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    5 ['pear', '8']
     please input item number:	2
    added ['flowers', 30] into shopping cart,your current balance is 1945
    1 ['watermelon', 25]
    2 ['flowers', 30]
    3 ['bottle', '15']
    4 ['flag', '11']
    5 ['pear', '8']
     please input item number:	3
    added ['bottle', 15] into shopping cart,your current balance is 1930
    1 ['watermelon', 25]
    2 ['flowers', 30]
    3 ['bottle', 15]
    4 ['flag', '11']
    5 ['pear', '8']
     please input item number:	q
    您已购买的产品有: ['watermelon', 'flowers', 'bottle']

    2.买爆输出结果:

    please input your salary:	20
    1 ['watermelon', '25']
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    5 ['pear', '8']
     please input item number:	1
    your current balance is not enough.
    1 ['watermelon', 25]
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    5 ['pear', '8']
     please input item number:	
    

      

    3.乱输东西的输出结果:

    please input your salary:	2000
    1 ['watermelon', '25']
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    5 ['pear', '8']
     please input item number:	fuck off
    invalid input
    1 ['watermelon', '25']
    2 ['flowers', '30']
    3 ['bottle', '15']
    4 ['flag', '11']
    5 ['pear', '8']
     please input item number:	
    

      

  • 相关阅读:
    ssm之spring+springmvc+mybatis整合初探
    mybatis缓存之整合第三方缓存工具ehcache
    mybatis缓存之二级缓存
    mybatis缓存之一级缓存
    mybatis动态sql之利用sql标签抽取可重用的sql片段
    mybatis动态sql之bind标签
    mybatis动态sql之内置参数_parameter和_databaseId
    mybatis动态sql之使用foreach进行批量插入的两种方式
    mybatis动态sql之foreach补充(三)
    Visitor Pattern
  • 原文地址:https://www.cnblogs.com/negu/p/11319859.html
Copyright © 2011-2022 走看看