= Li [{ ' name ' : ' phone ' , ' . price ' : ' 4999 ' }, { ' Name ' : ' computer ' , ' . Price ' : ' 10999 ' }, { ' Name ' : ' headset ' , ' . Price ' : ' 499 ' }, { ' Name ' : ' mouse ' , ' . Price ' : ' 299 ' }, { ' Name ' : ' keyboard ' , ' . Price ' : ' 599 ' }, ] shopping_car = {} # define cart dict Print ( ' Welcome Shunya dreams flagship store ' .center (40 )) # let customers enter the amount of money to carry, and then displays a list of goods (upgrade version can easily follow the recommended amount according to the number of different the price of the commodity, thousands of thousand faces) money = the iNPUT ( ' enter the amount of money you are carrying: ' ) .strip () # where the customer input string trailing spaces removed, use Strip f_money = int (money) for I, K in enumerate (Li,. 1): # use enumerate parameters traverse iterables list file acquired simultaneously indexes and values Print ( ' number {} \ t \ t name: {} \ t \ t price: {} element / a ' .format (I, K [ 'name ' ], K [ ' . price ' ])) # use format occupying In Flag = True the while In Flag: the Choose = the INPUT ( '' ' Please enter the number you want to purchase goods: Exit Press' q' or 'Q ' '' '' ) .strip () IF choose.upper () == ' Q ' : BREAK IF the Choose .isdigit () and int (the Choose) <= len (li): # serial number to determine whether the input NUM = the iNPUT ( ' Please enter the number you want to purchase ' ) .strip () iF num.isdigit (): iF int (Money)> int (Li [int (the Choose) -. 1] [ ' . price ' ]) * int (NUM): Money = int (Money) - int (li [int (the Choose) - 1] [ ' . price ' ]) * int (NUM) the else : Print ( ' Sorry less than the amount you are carrying, please add to shop before!. ' ) BREAK IF li [int (the Choose) - 1] [ ' name ' ] in shopping_car: Print ( ' '' your shopping cart has a {} {} '' ' .format (int (shopping_car [li [ int (the Choose) -. 1] [ ' name ' ]]), li[int(choose) - 1]['name'])) Order = INPUT ( '' ' Add another press' y 'or' Y ', in other reselect ' '' ) .strip () IF order.upper () == ' the Y ' : shopping_car[li[int(choose) - 1]['name']] = shopping_car[li[int(choose) - 1]['name']] + int(num) else: continue else: shopping_car [li [int (the Choose) - 1] [ ' name ' ]] = int (NUM) the else : Print ( ' Your input is incorrect, please re-enter: ' ) Print ( ' your shopping cart {there}, {this} consumer spending a total of RMB yuan balance of {} ' .format (shopping_car, f_money - Money, Money))
Upgraded version of the shopping cart code, according to the follow-up will continue to gradually improve the learning content update