購物車最佳化,
終於,終於,在自己這幾天無數次跳坑之後,終於爬出來了。把裡面所有的問題都修正了。
程式顯的有些繁瑣,但是對於我這個水平來說,已經是很高興了,畢竟學了並不是很久,供新手學習還是有些協助的。自己肯定也會越來越努力,好好學習,加油!
1 __Author__ = "Zhang Peng" 2 product_list = [ 3 ('Iphone', 5888), 4 ('筆記本', 12888), 5 ('手錶', 10600), 6 ('電視', 3888), 7 ('書', 58), 8 ] 9 10 id_list = ["商家", "使用者", "shangjia", "yonghu"] 11 id_shopping = input("Please input your ID:\n") 12 13 exit_flag = False 14 15 if id_shopping == "shangjia": 16 choice = input("你是否想添加\修改商品?添加商品輸入'1',修改商品輸入'2',退出輸入'q':\n") 17 if choice == "1": 18 while not exit_flag: 19 name = input("請輸入您想添加的商品名稱:\n") 20 name2 = input("請輸入您添加商品的價格:\n") 21 if name == "q" or name2 == "q": 22 exit_flag = True 23 else: 24 product_list.append((name, name2), ) 25 26 elif choice == "2": 27 while not exit_flag: 28 name3 = input("請輸入您想修改價格的商品名稱:\n") 29 name4 = input("請輸入您想要修改的價格:\n") 30 31 if name3 == "q" or name4 == "q": 32 exit_flag = True 33 else: 34 a = product_list.index(name3) 35 product_list.remove(name3) 36 product_list.index((name3, name4)) 37 38 else: 39 if choice == "q": 40 exit_flag = True 41 print(product_list) 42 43 44 elif id_shopping == "yonghu": 45 shopping_list=[] 46 with open("shopping_list.txt","r",encoding="utf8") as f_shop1: 47 print("你的購物車裡已經有:") 48 print(f_shop1.read()) 49 with open("salary.txt","r",encoding="utf8") as f_salary1: 50 salary = f_salary1.read() 51 print(salary) 52 if salary.isdigit(): # isdigit 是判斷輸入的是不是一個數字 53 salary = int(salary) 54 while True: 55 print("我們有一下商品:\n") 56 for index, item in enumerate(product_list): 57 print(index, item) 58 user_choice = input("請問你還想買什嗎?:\n") 59 if user_choice.isdigit(): 60 user_choice = int(user_choice) 61 if user_choice < len(product_list) and user_choice > 0: 62 p_item = product_list[user_choice] 63 if p_item[1] < salary: # 買不起 64 shopping_list.append(p_item) 65 salary -= p_item[1] 66 print("您買的商品已經加入購物車") 67 else: 68 print("沒錢別嘚瑟了") 69 else: 70 print("沒有您要的商品") 71 elif user_choice == 'q': 72 print("----------shopping list----------") 73 for p in shopping_list: 74 print(p) 75 with open("shopping_list.txt", "a", encoding="utf8") as f_shop2: 76 f_shop2.write(str(p)) 77 print("你還剩餘", salary) 78 with open("salary.txt", "w", encoding="utf8") as f_salary2: 79 f_salary2.write(str(salary)) 80 exit() 81 82 else: 83 print("輸入錯誤") 84 85 else: 86 shopping_list = [] 87 salary = input("請輸入你的月薪:\n") 88 if salary.isdigit(): # isdigit 是判斷輸入的是不是一個數字 89 salary = int(salary) 90 while True: 91 for index, item in enumerate(product_list): 92 print(index, item) 93 user_choice = input("請問你想買什嗎?:\n") 94 if user_choice.isdigit(): 95 user_choice = int(user_choice) 96 if user_choice < len(product_list) and user_choice > 0: 97 p_item = product_list[user_choice] 98 if p_item[1] < salary: # 買不起 99 shopping_list.append(p_item)100 salary -= p_item[1]101 print("您買的商品已經加入購物車")102 else:103 print("沒錢別嘚瑟了")104 else:105 print("沒有您要的商品")106 elif user_choice == 'q':107 print("----------shopping list----------")108 for p in shopping_list:109 print(p)110 with open("shopping_list.txt", "a",encoding="utf8") as f_shop3:111 f_shop3.write(str(p))112 print("你還剩餘", salary)113 with open("salary.txt", "w",encoding="utf8") as f_salary3:114 f_salary3.write(str(salary))115 exit()116 117 else:118 print("輸入錯誤")
希望能得到打什麼的評論,這才是我最大的學習動力!謝謝!