首页 > 代码库 > 小实验1:购物车记录

小实验1:购物车记录

 



1
shopping_list = [ 2 ("iphone",5800), 3 ("Mac_Pro",12000), 4 ("bike",1500), 5 ("bed",800), 6 ("cap",30), 7 ] 8 s_list=[] 9 use_salary = input("Please input you Salary:") 10 11 if use_salary.isdigit(): # 如果用户输入的是数字 12 use_salary = int(use_salary) # 赋值为整形数字 13 while True: # 如果输入数字为真 则进行循环 14 for index,item in enumerate(shopping_list,1): # index()表示数列中的位置,enumerate表示的就是数列中的下标 15 #print(shopping_list.index(item),item) 16 print(index,item) # index 就是下标,item 就是数列项 17 use_choice = input("What do you want ‘number‘,and Please input ‘q‘to exit:") 18 19 if use_choice.isdigit(): 20 use_choice = int(use_choice) # 判断输入的是否为数字 21 if use_choice > -1 and use_choice < len(shopping_list): # 如果取的数值在列表范围,才可以进行判断金额 22 shoping_item = shopping_list[use_choice] # 通过用户输入序号找到产品,而产品中包括名称和价格 23 if shoping_item[1] <= use_salary: 24 use_salary -= shoping_item[1] # shoping_item 表示表中价格列 25 s_list.append(shoping_item) 26 print("Add %s in you cart,you current balance is \033[31;1m %s \033[0m." %(shoping_item,use_salary)) 27 else: 28 print("You don‘t have enough money and you current balance is \033[31;1m%s\033[0m" %(use_salary)) 29 elif use_choice == "q": 30 print("------ list ------") 31 for i in s_list: 32 print(i) 33 print("your current balance is %s"%use_salary) 34 exit() 35 else: 36 print("not exist...")

自己之前构思了一个,不过不如这个清晰,还是要多写些,多联系,培养思路很重要...

Keep going !








小实验1:购物车记录