Python learning - shopping cart program

Keywords: Python Pycharm Mac

Procedure: shopping cart procedure

Demand:

  1. After starting the program, let the user enter the salary, and then print the list of products
  2. Allow users to purchase products according to the product number
  3. After the user selects the goods, check whether the balance is enough, deduct the money directly if it is enough, and remind if it is not enough
  4. Can exit at any time, when exiting, print the purchased goods and balance

The procedure is as follows:

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3 # @Time    : 2018/4/28 16:23
 4 # @Author  : yang
 5 # @File    : Shopping_Cart_Program01.py
 6 # @Software: PyCharm
 7 #Define product list
 8 product_list = [('Iphone',6000),
 9                 ('MAC Pro',9800),
10                 ('Bike',800),
11                 ('Watch',10600),
12                 ('Coffee',31),
13                 ('Alex python',120),]
14 shopping_list = []   #Define an empty shopping cart list
15 #Importation of wages
16 salary = input('Input your salary:')
17 if salary.isdigit():
18     salary = int(salary)
19     #Cycle the item number to be purchased
20     while True:
21         # enumerate() Function is used to traverse a data object(Such as list, tuple or string)Combine into an index sequence
22         for index,item in enumerate(product_list):    
23             print(index,item)      #Print out numbered product list
24         user_choice = input('Select item number to buy>>>: ')    #Enter item number to purchase
25         if user_choice.isdigit()==False and user_choice.upper() != 'Q':
26             print('\033[1;43m The item number you entered is illegal!\033[0m')
27             exit()
28         elif user_choice.isdigit()==True:       #Determine whether the input string is composed of numbers
29             user_choice = int(user_choice)
30             if user_choice < len(product_list) and user_choice >=0:
31                 p_item = product_list[user_choice]
32                 if p_item[1] <= salary:    #Afford
33                     shopping_list.append(p_item)
34                     salary -= p_item[1]
35                     print('Merchandise%s Add to cart, remaining amount\033[1;31;42m%s\033[0m'%(p_item,salary))
36                     #Highlight: Beginning:\033[Display mode;Foreground color;Background color m + End:\033[0m
37                 else:    #Cannot afford
38                     print('\033[1;41m Your balance is only[%s]Now, buy some wool!\033[0m'%salary)
39             else:
40                 print('\033[1;41m commodity%s Non-existent!\033[0m'%user_choice)
41 
42         #Exit shopping cart program: print out shopping list and balance
43         elif user_choice.upper() == 'Q':
44             print('---------------shopping list---------')
45             for p in shopping_list:
46                 print(p)
47             print('Your balance is up:',salary)
48             exit()
49         else:
50             exit()
51 else:     #If the salary entered is illegal, exit the program
52     print('The salary you entered is illegal!')
53     exit()

Note: for the program, please refer to Alex, the old boy, with blog address: http://www.cnblogs.com/alex3714/articles/5717620.html

Posted by timbuckthree on Sun, 29 Mar 2020 08:45:56 -0700