Example of a shopping cart program exercise (recommended) and a Python shopping cart at the beginning of python
Let's just talk about the code.
# Author: Lancy Wuproduct_list = [('iphone ', 5800), ('mac Pro', 9800), ('bike', 800), ('Watch', 10600 ), ('coffee ', 31), ('lancy python', 120)] # product list shopping_list = [] # define a list to store the purchased product salary = input ("Enter the salary:") if salary. isdigit (): # When the input content is digit salary = int (salary) # convert the input salary to int type while True: # print all the items in a loop, there are two ways to write the statement: # for item in product_list: # print (product_list.index (item), item) for index, item in enumerate (prod Uct_list): # enumerate () This method is to retrieve the list subscript print (index, item) user_choice = input ("do you want to buy a product? If you want to purchase a product, enter the product number: ") if user_choice.isdigit (): # When the product number is user_choice = int (user_choice) # convert the input product number to int type if user_choice <len (product_list) and user_choice> = 0: # determine whether the input item number exists p_item = product_list [user_choice] # retrieve the purchased item if p_item [1] <= salary: # When the product price is less than or equal to the balance shopping_list.append (p_item) # store purchased items in the shopping_list [] list salary-= p_item [1] # Calculate the balance print ("the purchased item is % s, and the balance is \ 033 [31; 1 m % s \ 033 [0 m "% (p_item, salary) # This location After processing, the balance is highlighted by else: # When the price of the product is higher than the balance print ("\ 033 [41; 1 m your balance is only [% s] \ 033 [0 m "% salary) # This place has been processed, the balance is highlighted else: print (" This item does not exist! ") Elif user_choice = 'q': # When the input product number is q, print the purchased item and balance and exit the program print ("-------- The following is the purchased item --------") for p in shopping_list: print (p) print ("your balance is :", salary) exit () else: print ("This item does not exist! ")
The above example of the shopping cart program exercise (recommended) in the beginning of Python is all the content shared by the editor. I hope to give you a reference and support for the help house.