第2周作业1

Posted ld1977

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第2周作业1相关的知识,希望对你有一定的参考价值。

1.优化购物程序,购买时允许用户选择购买多少件,

2.允许多用户登录,下一次登录后,继续按上次的余额继续购买 ,可以充值 (每个用户有独立的保存文件)

3.允许用户查看之前的购买记录(记录要显示商品购买时间)

4.商品列表分级展示,比如:

  第一层菜单:

  1.家电类

  2.衣服类

  3.手机类

  4.车类

  ...

  选择一个,车类,进入第2层

  1。BMW X3 33333

  2.Audi Q5 33333

  3.Pasate 33335

  4.Tesla Model_3 430000

  5.Tesla Model S 888888

购物车:

1. 商品信息- 数量、单价、名称

2. 用户信息- 帐号、密码、余额

3. 用户可充值

4. 购物历史信息

5. 允许用户多次购买,每次可购买多件

6. 余额不足时进行提醒

7. 用户退出时 ,输出档次购物信息

8. 用户下次登陆时可查看购物历史

9. 商品列表分级显示

 

 5.显示已购买商品时,如果有重复的商品,不打印多行,而是在一行展示,如

  id   p_name        num    total_price

        1.  TeslaModelS   2         3434343434

   2.  Coffe        2   60

   3. Bike         1         700

###

Blog一定要写

readme一定要写

流程图 一定画

一定不要copy

会用到 文件 ,datetime模块,json

折磨啊,已经被搞晕,还有需要完善的地方。

 

#!/usr/bin/env python
# -*-coding:utf-8-*-
# __author__="Liudong"
import json
if __name__ == \'__main__\':
    # user_info={\'root\':[123,5000],\'sys\':[123,10000],\'admin\':[123,200000]}
    # json.dump(user_info, open(\'user_info.txt\',\'w\'))#把用户信息以user:pass:amount的格式写入文本文件
    # purchase_log={\'root\':[[\'iphone7\' ,2 ,\'2016-6-10\'],[\'西装\',1 ,\'2015-5-2\']],
    #             \'sys\':[[\'乐视电视\',1,\'2016-9-20\'] ,[\'华为\',5,\'2016-7-15\']],
    #             \'admin\':[[\'乐视电视\',1,\'2016-8-10\'] ,[\'运动服\',3,\'2016-10-10\']]
    #              }
    # json.dump(purchase_log,open(\'purchase_log.txt\',\'w\',encoding=\'utf-8\'))

    shop_car = []
    welcom_msg = \'Welcome to Shopping Home\'.center(50, \'-\')
    print(welcom_msg)
    for i in range(3):
        username = input(\'Please input your username:[q=quit]\')
        if username == \'q\':
            exit()
        with open(\'user_info.txt\', \'r\', encoding=\'utf-8\') as user_account:  # 读取用户信息
            user_info = json.load(user_account)  # 用json读取文件,字典格式
        for user_name in user_info.keys():  # 判断用户名是否存在
            # print(user_info[user_name][0])
            # print(user_name)
            # print(username)
            j = 0
            while j < 3:  # 给3次输入密码机会
                password = input(\'Please input your password:\')
                if password == str(user_info[user_name][0]):
                    print(\'login successed! Have a happy shopping Time!\')
                    while True:
                        print(\'#\' * 50)
                        print(\'1.查看购买记录\\n2.充值\\n3.查询余额\\n4.购物\\n5.查看购物车\\n[q:quit]\')
                        user_want = input(\'What do you do?\')
                        if user_want == \'q\':
                            exit()
                        if user_want == \'1\':
                            print(\'user %s purchase log list:\'.center(50, \'*\') % user_name)
                            with open(\'purchase_log.txt\', \'r\', encoding=\'utf-8\') as purchase_log:  # 读取购买记录文件
                                user_purchase_log = json.load(purchase_log)
                                print(user_purchase_log[username])
                        if user_want == \'2\':
                            print(\'*\' * 50)
                            charge_amount = input(\'please input amount you want to charge:\')
                            # user_info_amountw = json.load(\'user_info.txt\', \'r\')
                            user_info[username][1] += int(charge_amount)
                            # print(user_info)
                            json.dump(user_info, open(\'user_info.txt\', \'w\'))
                            print(\'charge successed!\')
                        if user_want == \'3\':
                            print(\'*\' * 50)
                            print(\'your account is ¥%s\' % user_info[user_name][1])
                        if user_want == \'4\':
                            product_list = {
                                \'家电类\': [(\'乐视电视\', 7000), (\'海尔冰箱\', 5000), (\'小天鹅洗衣机\', 2000)],
                                \'衣服类\': [(\'西装\', 8000), (\'休闲夹克\', 2000), (\'运动服\', 800)],
                                \'手机类\': [(\'Iphone7\', 7000), (\'小米Note\', 2000), (\'华为\', 1000)],
                                \'车类\': [(\'奔驰ML\', 800000), (\'特斯拉\', 700000), (\'奥迪Q7\', 750000)]}
                            class_list = []  # 获取序列号和字典key组成的列表
                            class2 = []
                            while True:
                                print("Products list".center(50, \'-\'))
                                for index, item in enumerate(product_list):  # 打印序列号和类别
                                    print(index, item)
                                    class_list = [index, item]  # 将序列号和商品类别key保存成列表
                                    class2.append(str(class_list[1]))  # class2用于保存用户选择商品类key值
                                # print(class2)                             #存储类别用于后面选择对应类别
                                print(\'End\'.center(50, \'-\'))
                                class1_choice = input(\'choose class number 0-3 to enter:(q:quit)\').strip()
                                if class1_choice == \'0\':  # 用户选择第一类商品
                                    class_choice = product_list[class2[0]]  # 根据商品类别和对应的下标号,获取商品列表
                                    # print(class_choice)
                                    for item in enumerate(class_choice):  # 从第1级类中,枚举各个商品
                                        index = item[0]
                                        # print(item)
                                        p_name = item[1][0]
                                        p_price = item[1][1]
                                        print(index, \'.\', p_name, p_price)
                                    buy_item = input(\'What do you want to buy?:[q=quit,c=check]\')
                                    if buy_item.isdigit():  # 肯定为选择商品
                                        buy_item = int(buy_item)
                                        if buy_item < len(class_choice):
                                            # print(class1_choice)
                                            # print(class_list)
                                            p_item = [p_name[1]]
                                            # print(p_item)
                                            # print(user_info[user_name][1])
                                            if p_price <= user_info[user_name][1]:  # 买的起
                                                shop_car.append(p_item)  # 放入购物车
                                                user_info[user_name][1] -= p_price  # 扣钱
                                                print(
                                                    \'Added [%s] into your shop car,your current balance is \'
                                                    \'\\033[31;1m[%s]\\033[0m\' % (p_item, user_info[user_name][1]))
                                                # 字体加颜色
                                            else:
                                                print(\'Your balance is [%s],cannot afford this product..\' %
                                                      user_info[user_name][1])

                                    else:
                                        if class1_choice == \'q\' or class1_choice == \'quit\':
                                            print(\'purchased products as blew:\'.center(40, \'*\'))
                                            for item in shop_car:
                                                print(item)
                                                print(\'END\'.center(40, \'*\'))
                                                print(\'Your balance is [%s]\' % user_info[user_name][1])
                                                print(\'bye.\')
                                            exit()
                                        elif class1_choice == \'c\' or class1_choice == \'check\':
                                            print(\'purchased products as blew:\'.center(40, \'*\'))
                                            for item in shop_car:
                                                print(item)
                                            # 背景加颜色
                                            print(\'Your balance is \\033[41;1m[%s]\\033[0m\' % user_info[user_name][1])
                                if class1_choice == \'1\':  # 用户选择第一类商品
                                    class_choice = product_list[class2[0]]  # 根据商品类别和对应的下标号,获取商品列表
                                    # print(class_choice)
                                    for item in enumerate(class_choice):  # 从第1级类中,枚举各个商品
                                        index = item[0]
                                        # print(item)
                                        p_name = item[1][0]
                                        p_price = item[1][1]
                                        print(index, \'.\', p_name, p_price)
                                    buy_item = input(\'What do you want to buy?:[q=quit,c=check]\')
                                    if buy_item.isdigit():  # 肯定为选择商品
                                        buy_item = int(buy_item)
                                        if buy_item < len(class_choice):
                                            # print(class1_choice)
                                            # print(class_list)
                                            p_item = [p_name[1]]
                                            # print(p_item)
                                            # print(user_info[user_name][1])
                                            if p_price <= user_info[user_name][1]:  # 买的起
                                                shop_car.append(p_item)  # 放入购物车
                                                user_info[user_name][1] -= p_price  # 扣钱
                                                print(
                                                    \'Added [%s] into your shop car,your current balance is \'
                                                    \'\\033[31;1m[%s]\\033[0m\' % (p_item, user_info[user_name][1]))
                                                # 字体加颜色
                                            else:
                                                print(\'Your balance is [%s],cannot afford this product..\' %
                                                      user_info[user_name][1])

                                    else:
                                        if class1_choice == \'q\' or class1_choice == \'quit\':
                                            print(\'purchased products as blew:\'.center(40, \'*\'))
                                            for item in shop_car:
                                                print(item)
                                                print(\'END\'.center(40, \'*\'))
                                                print(\'Your balance is [%s]\' % user_info[user_name][1])
                                                print(\'bye.\')
                                            exit()
                                        elif class1_choice == \'c\' or class1_choice == \'check\':
                                            print(\'purchased products as blew:\'.center(40, \'*\'))
                                            for item in shop_car:
                                                print(item)
                                            # 背景加颜色
                                            print(\'Your balance is \\033[41;1m[%s]\\033[0m\' % user_info[user_name][1])
                                if class1_choice == \'2\':  # 用户选择第一类商品
                                    class_choice = product_list[class2[0]]  # 根据商品类别和对应的下标号,获取商品列表
                                    # print(class_choice)
                                    for item in enumerate(class_choice):  # 从第1级类中,枚举各个商品
                                        index = item[0]
                                        # print(item)
                                        p_name = item[1][0]
                                        p_price = item[1][1]
                                        print(index, \'.\', p_name, p_price)
                                    buy_item = input(\'What do you want to buy?:[q=quit,c=check]\')
                                    if buy_item.isdigit():  # 肯定为选择商品
                                        buy_item = int(buy_item)
                                        if buy_item < len(class_choice):
                                            # print(class1_choice)
                                            # print(class_list)
                                            p_item = [p_name[1]]
                                            # print(p_item)
                                            # print(user_info[user_name][1])
                                            if p_price <= user_info[user_name][1]:  # 买的起
                                                shop_car.append(p_item)  # 放入购物车
                                                user_info[user_name][1] -= p_price  # 扣钱
                                                print(
                                                    \'Added [%s] into your shop car,your current balance is \'
                                                    \'\\033[31;1m[%s]\\033[0m\' % (p_item, user_info[user_name][1]))
                                                # 字体加颜色
                                            else:
                                                print(\'Your balance is [%s],cannot afford this product..\' %
                                                      user_info[user_name][1])

                                    else:
                                        if class1_choice == \'q\' or class1_choice == \'quit\':
                                            print(\'purchased products as blew:\'.center(40, \'*\'))
                                            for item in shop_car:
                                                print(item)
                                                print(\'END\'.center(40, \'*\'))
                                                print(\'Your balance is [%s]\' % user_info[user_name][1])
                                                print(\'bye.\')
                                            exit()
                                        elif class1_choice == \'c\' or class1_choice == \'check\':
                                            print(\'purchased products as blew:\'.center(40, \'*\'))
                                            for item in shop_car:
                                                print(item)
                                            # 背景加颜色
                                            print(\'Your balance is \\033[41;1m[%s]\\033[0m\' % user_info[user_name][1])
                                if class1_choice == \'3\':  # 用户选择第一类商品
                                    class_choice = product_list[class2[0]]  # 根据商品类别和对应的下标号,获取商品列表
                                    # print(class_choice)
                                    for item in enumerate(class_choice):  # 从第1级类中,枚举各个商品
                                        index = item[0]
                                        # print(item)
                                        p_name = item[1][0]
                                        p_price = item[1][1]
                                        print(index, \'.\', p_name, p_price)
                                    buy_item = input(\'What do you want to buy?:[q=quit,c=check]\')
                                    if buy_item.isdigit():  # 肯定为选择商品
                                        buy_item = int(buy_item)
                                        if buy_item < len(class_choice):
                                            # print(class1_choice)
                                            # print(class_list)
                                            p_item = [p_name[1]]
                                            # print(p_item)
                                            # print(user_info[user_name][1])
                                            if p_price <= user_info[user_name][1]:  # 买的起
                                                shop_car.append(p_item)  # 放入购物车
                                                user_info[user_name][1] -= p_price  # 扣钱
                                                print(
                                                    \'Added [%s] into your shop car,your current balance is \'
                                                    \'\\033[31;1m[%s]\\033[0m\' % (p_item, user_info[user_name][1]))
                                                # 字体加颜色
                                            else:
                                                print(\'Your balance is [%s],cannot afford this product..\' %
                                                      user_info[user_name][1])

                                    else:
                                        if class1_choice == \'q\' or class1_choice == \'quit\':
                                            print(\'purchased products as blew:\'.center(40, \'*\'))
                                            for item in shop_car:
                                                print(item)
                                                print(\'END\'.center(40, \'*\'))
                                                print(\'Your balance is [%s]\' % user_info[user_name][1])
                                                print(\'bye.\')
                                            exit()
                                        elif class1_choice == \'c\' or class1_choice == \'check\':
                                            print(\'purchased products as blew:\'.center(40, \'*\'))
                                            for item in shop_car:
                                                print(item)
                                            # 背景加颜色
                                            print(\'Your balance is \\033[41;1m[%s]\\033[0m\' % user_info[user_name][1])

                        if user_want == \'5\':
                            print(shop_car)
                else:
                    print(\'Invalid username or password...\')
                    print(\'this is the %d time(s)\' % (j + 1))
                    j += 1
            else:
                print(\'Forget password? Please send a mail to Administrator,bye\')
                user_account.close()
                exit()
View Code

 

以上是关于第2周作业1的主要内容,如果未能解决你的问题,请参考以下文章

201621123057 《Java程序设计》第9周学习总结

201621123062《java程序设计》第九周作业总结

《网络攻防第十周作业》

201621123037 《Java程序设计》第9周学习总结

第18周作业

2017-2018-2 20179215《网络攻防实践》第十周作业