python shopping cart functionality

demand:

  1. After starting the program that allows the user to enter wages, and then print the Product List
  2. Allow users to purchase by product number
  3. After the user selects a product, testing the balance is enough, enough to direct debit, is not enough to remind 
  4. Can withdraw at any time, when you exit, print the purchase of goods and balances
. 1 product_list = [
 2      ( ' Iphone ' , 6000 ),
 . 3      ( ' Watch ' , 2000 ),
 . 4      ( ' Bike ' , 1000 ),
 . 5      ( ' Book ' , 80 ),
 . 6      ( ' Coffee ' , 30 ),
 . 7  ]
 8 the shopping_list = []
 9  the while True:
 10      salary = the INPUT ( " how much do you pay:" )
 . 11      IF salary.isdigit ():
 12 is          the salary = int (the salary)
 13 is          the while True:
 14              # cycles to print out a list of commodity 
15              for index, Item in the enumerate (product_list):
 16                  Print (index, Item)
 . 17              USER_CHOICE = INPUT ( " enter a product serial number, select goods that you want to buy: " )
 18 is              iF user_choice.isdigit ():
 . 19                  USER_CHOICE = int (USER_CHOICE)
 20 is                  # determines whether there is an input sequence number 
21 is                  iF USER_CHOICE <len (product_list)and USER_CHOICE> = 0:
 22 is                      p_item = product_list [USER_CHOICE]
 23 is                      # determines whether enough money 
24                      IF the salary> = p_item [. 1]: # afford 
25                          shopping_list.append (p_item)
 26 is                          the salary - p_item = [. 1 ]
 27                          Print ( " add to cart% s success, your remaining balance% s " % (p_item, salary))
 28                      the else :
 29                          Print ( " lack of balance " )
 30                  the else :
 31                      Print( " Input error, you select the products do not present " )
 32              elif USER_CHOICE == ' Q ' :
 33 is                  Print ( " -------- ----------- shopplist " )
 34 is                  for P in the shopping_list:
 35                      Print (P)
 36                  Print ( " your balance is S% " % (the salary))
 37 [                  Exit ()
 38 is              the else :
 39                  Print ( ' input error ' )
 40      the else :
41 is          Print ( " Enter a number " )
View Code

note:

isdigit () determines whether the number input by the user
the enumerate () function is used to traverse a data object (such as a list, string, or tuples) as a combination index sequence, while the data lists and data standard, which is generally used in a for loop.

the enumerate ( Sequence , [ Start = 0 ]) Parameters: 


Sequence - A sequence, iteration iterator objects or other support.

start - start index position.

example:
1 >>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
2 >>> list(enumerate(seasons))
3 [(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
4 >>> list(enumerate(seasons, start=1))       # 下标从 1 开始
5 [(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
View Code

 

Guess you like

Origin www.cnblogs.com/xifeng59/p/11649443.html