博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
作业二:优化购物车:用户入口:1.将商品的信息存到文件中;2.将已经购买的商品、余额记录存到文件中。商家入口:1.可以添加商品;2.可以修改商品的价格...
阅读量:4983 次
发布时间:2019-06-12

本文共 2951 字,大约阅读时间需要 9 分钟。

#Author:AXIN#Date:2017/5/22 12:04#优化版的购物车#用户入口:#1.商品的信息存到文件里#2.已购商品,余额记录#商家入口:#1.可以添加商品#2.修改商品价格product_list = [    ('Iphone',5288),    ('Mac pro',12000),    ('Bike',800),    ('Watch',36000),    ('Coffe',39),    ('Python book',120),] #将商品信息打印到console窗口下def print_write_product():    print_list()    # 将商品信息写入product.txt文件    f = open("product.txt", 'w')    f.write(str(product_list) + "\n")    f.close()#用户输入自己的薪水def choose_product():    salary = input("Please input your salary :")    if salary.isdigit():        salary = int(salary)    #用户选择要购买的商品    shopping_list = []    while True:        user_choice = input('Please input your wanna product number :')        if user_choice.isdigit():            user_choice = int(user_choice)            if user_choice >=0 and user_choice <= len(product_list):                p_item = product_list[user_choice]                if salary >=  p_item[1]:                    shopping_list.append(p_item)                    salary -= p_item[1]                    print("Added %s into shopping cart,your current balance is %s " % (p_item, salary))                else:                    print('Your salary is not enough !Your balabce only [%s] !'%salary)            else:                print("Product code [%s] is not exist !" % user_choice)        elif user_choice =='q':            f = open("shopping_record.txt", 'w')            print('--------------shopping_list---------------')            for p in shopping_list:  # python 中直接使用的变量初始化的 就是0,不用定义再使用                print(p)                f.write(str(p) + "\n")            f.write(str(salary) + "\n")            print('Your balance :', salary)            f.close()            print('exit....')            exit()        else:            print('Invalide choice ')#专门给商家提供的入口:#增加货物def add_product(tuple):    product_list.append(tuple)    print_list()#修改价格def modify_price(i):    print('Input the changed product and price:')    product_list[i] = input(tuple)    print_list()#请用户输入自己是商家还是用户def identity():    i = input('business-----b''\n'              'client-------c''\n')    return i#商家可执行菜单选项def select_service():    i = input('adding goods-----a''\n'              'change the price of the product-------c''\n')    return idef print_list():    for index, item in enumerate(product_list):  # enumerate 能把下标取出来        print(index, item)#主函数i = identity()if i == 'b':    j = select_service()    if j == 'a':        print_list()        a = input('Input adding product and price:')        add_product(a)        f = open("c_product.txt", 'w')        f.write(str(product_list) + "\n")        f.close()    elif j == 'c':        print_list()        c = int(input('Input what you wanna changed number:'))        modify_price(c)        f = open("c_product.txt", 'w')        f.write(str(product_list) + "\n")        f.close()elif i == 'c':    print_write_product()    choose_product()else:    print('Invalied input !')

  

转载于:https://www.cnblogs.com/AXINblogs/p/6890950.html

你可能感兴趣的文章