1. 程式人生 > >作業二:優化購物車:用戶入口:1.將商品的信息存到文件中;2.將已經購買的商品、余額記錄存到文件中。商家入口:1.可以添加商品;2.可以修改商品的價格

作業二:優化購物車:用戶入口:1.將商品的信息存到文件中;2.將已經購買的商品、余額記錄存到文件中。商家入口:1.可以添加商品;2.可以修改商品的價格

car brush 薪水 blog and not else iphone client

#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 i def 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 !‘)

  

作業二:優化購物車:用戶入口:1.將商品的信息存到文件中;2.將已經購買的商品、余額記錄存到文件中。商家入口:1.可以添加商品;2.可以修改商品的價格