Class Exercise-python shopping cart, class-python shopping cart
1 #! /Usr/bin/env python 2 # _ author: bruce 3 # @ DATe: 2017-09-08 4 5 proud_list = [6 ('iphone6s ', 6800), 7 ('ipad ', 3000), 8 ('mac Book', 9800) 9] 10 # receive the user input the existing money amount 11 money = input ("please input your money :") 12 # shopping cart list 13 sp = [] 14 15 # determine whether the user input is valid 16 if money. isdigit (): 17 money = int (money) 18 while True: 19 # print the product list 20 for I, v in enumerate (proud_list, 1): 21 print (I ,": ", v) 22 # user input, select product list 23 choice = input (" Enter the serial number to select Select Product (q, exit): ") 24 # judge whether user input is valid 25 if choice. isdigit (): 26 choice = int (choice) 27 if choice> 0 and choice <= len (proud_list ): 28 # obtain the item you entered 29 p_item = proud_list [choice-1] 30 if p_item [1] <money: 31 money-= p_item [1] 32 sp. append (p_item) 33 else: 34 print ("insufficient balance! The balance is % s RMB "% money) 35 print (p_item) 36 else: 37 print (" the product serial number does not exist! ") 38 elif choice = 'q': 39 print (" -------- you have purchased the following products -------- ") 40 # print the purchased products 41 for I in sp cyclically: 42 print (I) 43 print ("You still have % s RMB", money) 44 break45 else: 46 print ("input error ")