Shopping cart function to achieve an optimized version ----

Simple version of the first edition:

the while True: 
 sar = the INPUT ( ' Enter salary: ' )
  IF sar.isdigit (): 
     sar = int (sar)
      BREAK 
 the else :
      Print ( ' Please enter the correct format! ' ) 
TH = [[ ' MacBook ' , 7000] , [ ' iPhone ' , 5000], [ ' Coffee ' , 90], [ ' pythonbook ' , 40 ]]
 for I in TH:
     Print (th.index (I), I) 
LIS =[] 
In Flag = ' T ' 
the while ! In Flag = ' Q ' : 
    S = int (INPUT ( ' Enter the product number you need: ' )) 
    K = 0
     for J in LIS: 
        K = K + J [. 1 ]
     IF int (TH [S] [. 1]) + int (K)> SAR:
         Print ( ' insufficient funds ' )
     the else : 
        lis.append (TH [S]) 
        SAR = Sar- K
         Print ( ' the current balance is:% d' % (Sar-int (TH [S] [1]) - int (k))) 
    Flag = the INPUT ( ' ? Also buy it the y-/ q ' )
     IF Flag == ' q ' :
         Print ( ' You have purchased: ' )
         for m in LIS:
             Print (m)
         Print ( ' your balance is% D ' % (SAR-int (TH [S] [. 1]) - int (K)))
         Print ( ' Welcome come again ' )

---- Second Edition modified knowledge: (1) a list of tuples and a plurality of variables can be accepted, so that ease of operation (2) adding a product number determination

the while True: 
 sar = the INPUT ( ' Enter salary: ' )
  IF sar.isdigit (): 
     sar = int (sar)
      BREAK 
 the else :
      Print ( ' Please enter the correct format! ' ) 
TH = [[ ' MacBook ' , 7000] , [ ' iPhone ' , 5000], [ ' Coffee ' , 90], [ ' pythonbook ' , 40 ]] 

for I, V in the enumerate (TH,. 1 ):
     Print (I, '>>>> ' , v) 

# The above is the realization of the input wages are reasonable judgment unreasonable to re-enter 
# commodity order to make a judgment entered: 1 non-numeric Exit 2. 3. 4. exceed the length of the embedded space --- Analyzing sleeve 
LIS = []
 the while True: 
    S = iNPUT ( ' enter the product number you need to exit enter Q: ' )
     IF s.isdigit () and 0 <= int (S) < len (TH): 
        S = int (S)
         IF SAR> TH [S] [. 1 ]: 
            lis.append (TH [S]) 
            SAR = SAR - TH [S] [. 1 ]
             Print ( ' the current balance is: D% ' % (SAR) )
         the else:
             Print ( ' current lack of balance ' )
     elif S == ' q ' :
         IF LIS == []:
             Print ( ' you did not purchase any goods ' )
             BREAK 
        the else :
             Print ( ' the item you've purchased: ' )
             for m in LIS:   # traverses purchased 
                Print (m)
             Print ( ' your balance is d% ' % (sar), ' Welcome back' )
             BREAK 
    the else :
         Print ( " Please enter the correct number ' )

 

Guess you like

Origin www.cnblogs.com/duguxiren/p/11622601.html