购物车小程序
1 salary = input("Input your salary:") 2 if salary.isdigit(): 3 salary = int(salary) 4 else: 5 exit("Invaild data type") 6 welcome_msg = "Welcome to Liangml shopping mall".center(50,"-") 7 print(welcome_msg) 8 exit_flag = False 9 product_list = [ 10 ("Iphone",5888), 11 ("Mac book",8888), 12 ("Mac Air",12345), 13 ("book",20), 14 ("Dell",12345), 15 ("Coffe",10)] 16 shop_car = [] 17 while not exit_flag: 18 #for product_item in product_list: 19 # p_name,p_price = product_item 20 print("product list".center(50,"-")) 21 for item in enumerate(product_list): 22 index = item[0] 23 p_name = item[1][0] 24 p_price = item [1][1] 25 print(index,".",p_name,p_price) 26 user_choice = input("[q=quit,c=check]What do you wang to buy?:") 27 if user_choice.isdigit():#肯定是选商品 28 user_choice = int(user_choice) 29 if user_choice < len(product_list): 30 p_item = product_list[user_choice] 31 if p_item[1] <= salary:#买的起 32 shop_car.append(p_item)#放入购物车 33 salary -= p_item[1]#扣钱 34 print("Added [%s] into shop car,you current balance is [%s]"% 35 (p_item,salary)) 36 else: 37 print("You balance is [%s],cannot afford this...."%salary) 38 else: 39 if user_choice == "q" or user_choice == "quit": 40 print("purchased products as below".center(40,"*")) 41 for item in shop_car: 42 print(item) 43 print("END".center(40,"*")) 44 print("Your balace is [%s]"% salary) 45 exit_flag = True