A simple Shopping Cart program and a simple Shopping Cart program

Source: Internet
Author: User

A simple Shopping Cart program and a simple Shopping Cart program

A few days ago, I followed the video and made a simple Shopping Cart program, but I didn't make it. Then I followed the video and made a simple Shopping Cart program. The teacher in the video also told me that I had to keep trying to write it, you can feel it slowly. The less you write, the less you will not. The following is a comparison between the shopping cart I wrote a few days ago and the shopping cart I followed the video:

Self-written:

1 _ Author _ = "Zhang Peng" 2 3 salary = input ("What is your monthly salary? \ N ") 4 somethings = {" IPhone ": 5888," Cup ": 8," Glasses Box ": 15," Calculator ": 38," Mouse ": 128, "computer": 7889, "snack": 120,} 5 Shopping_Cart = [] 6 7 while True: 8 dongxi = input ("What do you want to buy? \ N ") 9 10 if dongxi = 'quit': 11 break12 13 elif somethings [dongxi] = 'keyerror': 14 print (" Sorry! The product you want is not found. ") 15 break16 17 elif salary-somethings [dongxi]> = 0: 18 Shopping_Cart.append ([dongxi, somethings [dongxi],) 19 salary = salary-somethings [dongxi] 20 21 elif salary-somethings [dongxi] <0: 23 print ("The balance you have left is not enough to buy it. try something else, please. ") 24 print (" Enter 'quit' to end the program. ") 25 26 27 28 29 print (F' you still have {salary} RMB! ') 30 print (f'your shopping list: \ n {Shopping_Cart }')


Written by the Instructor:

Product_list = [('iphone ', 5800), ('mac Pro', 9800), ('bike', 800), ('Watch', 10600 ), ('coffee ', 31), ('Alex python', 120),] shopping_list = [] salary = input ("Input your salary:") if salary. isdigit (): salary = int (salary) while True: for index, item in enumerate (product_list): # print (product_list.index (item), item) print (index, item) user_choice = input ("select to buy? >>>: ") If user_choice.isdigit (): user_choice = int (user_choice) if user_choice <len (product_list) and user_choice> = 0: p_item = product_list [user_choice] if p_item [1] <= salary: # Buy shopping_list.append (p_item) salary-= p_item [1] print ("Added % s into shopping cart, your current balance is \ 033 [31; 1 m % s \ 033 [0 m "% (p_item, salary) else: print (" \ 033 [41; 1 m your balance is only [% s], and you still need to buy a wool \ 033 [0 m "% salary) else: print (" Product code [% s] is not exist! "% User_choice) elif user_choice = 'q': print (" -------- shopping list ------ ") for p in shopping_list: print (p) print (" Your current balance :", salary) exit () else: print ("invalid option ")

The subsequent video assignment is to optimize the shopping cart. The optimized content is to add users and merchant portals respectively. Users can display their previous shopping records and their wallet balances; sellers can add commodities and modify commodity prices. The following code is written by myself. The storage and calling methods in the future are still not skilled and the writing is messy, I tried it many times, but I still cannot. You can check it out:

Product_list = [('iphone ', 5888), ('laptop', 12888), ('Watch ', 10600), (' TV ', 3888), ('book ', 58),] id_list = ["Merchant", "user", "shangjia", "yonghu"] id_shopping = input ("Please input your ID: \ n ") exit_flag = False if id_shopping = "shangjia": choice = input ("do you want to add or modify a product? Add product input '1', modify product input '2', and exit the input 'q': \ n ") if choice =" 1 ": while not exit_flag: name = input ("enter the name of the product you want to add: \ n") name2 = input ("Enter the price of the product you want to add: \ n ") if name = "q" or name2 = "q": exit_flag = True else: product_list.append (name, name2),) elif choice = "2 ": while not exit_flag: name3 = input ("Enter the product name you want to change the price: \ n") name4 = input ("Enter the price you want to change: \ n ") if name3 = "q" or name4 = "q": exit_flag = True else: a = product_list.index (name3) product _ List. remove (name3) product_list.index (name3, name4) else: if choice = "q": exit_flag = True print (product_list) elif id_shopping = "yonghu ": shopping_list = [] with open ("shopping_list.txt") as f: print ("your shopping cart already has: \ n") f. read () with open ("salary.txt") as f: salary = f. read () print (salary) if salary. isdigit (): # isdigit is used to determine whether the input is a number salary = int (salary) while True: print ("we have the following products: \ n") for index, item in enum Erate (product_list): print (index, item) user_choice = input ("What else do you want to buy?: \ N ") if user_choice.isdigit (): user_choice = int (user_choice) if user_choice <len (product_list) and user_choice> 0: p_item = product_list [user_choice] if p_item [1] <salary: # shopping_list.append (p_item) salary-= p_item [1] print ("Your purchased item has been added to the shopping cart ") else: print ("no money, don't worry") else: print ("no product you want") elif user_choice = 'q ': print ("---------- shopping list ----------") for p in shopping_list: print (p) with open ("sh Opping_list.txt "," w ") as f: f. write ("shopping_list") print ("You still have", salary) salary. save ("salary.txt") else: print ("input error") else: shopping_list = [] salary = input ("Enter your monthly salary: \ n") if salary. isdigit (): # isdigit is used to determine whether the input is a number salary = int (salary) while True: for index, item in enumerate (product_list): print (index, item) user_choice = input ("what do you want to buy?: \ N ") if user_choice.isdigit (): user_choice = int (user_choice) if user_choice <len (product_list) and user_choice> 0: p_item = product_list [user_choice] if p_item [1] <salary: # shopping_list.append (p_item) salary-= p_item [1] print ("Your purchased item has been added to the shopping cart ") else: print ("no money, don't worry") else: print ("no product you want") elif user_choice = 'q ': print ("---------- shopping list ----------") for p in shopping_list: print (p) shopping_list.save ("shopping_list.txt") print ("You still have", salary) with open ("salary.txt", "w") as f: f. write ("salary") exit () else: print ("input error ")
View Code

If you are interested, please take a look. welcome to join us. Give me some suggestions. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.