购物车
products = [ #定义商品列表 ["Ipone",5800], ["Mac",15800], ["Coffee",30], ["Bike",2000], ["Cloth",500], ]
shopping_list = [] #空的购物车 while True: #循环输入工资 salary = input("Your salary:") if salary.isdigit(): #判断输入是否为整型整数 salary = int(salary) #将字符串转化为数字 break #正确执行就退出 else: continue #错误就循环执行 while True: print("product list".center(50,"-")) #以 “-“ 为填充居中打印 “product list” center:居中打印 for index,i in enumerate(products): #打印商品列表、价格 print(index,".",i[0],i[1]) choice = input("请输入商品编号[quit]>>:") if choice.isdigit(): choice = int(choice) if choice >=0 and choice < len(products): #判断列表长度:len #判读钱够不够 p = products[choice] if salary >= p[1]:#买的起 salary -= p[1] #扣钱 shopping_list.append(p) #加入购物车 print("Added \033[32;1m[%s]\033[0m into your shopping cart,and your current balance is \033[41;1m%s\033[0m" %(p[0],salary)) #彩色打印 else: print("钱不够,你只有[%s]" % salary) else: print("没有此商品...") elif choice == "quit": #输入quit退出 print("已购买商品".center(50,"-")) for i in shopping_list: print(i) #打印购物车 print("Your left balance is ", salary) #打印剩余余额 exit()