Python in Shopping cart practice
Requirements: You can enter a salary, select a commodity, and as long as the user has enough money to buy the goods, until the balance is insufficient, after the purchase is completed format the printing user's balance and merchandise.
#_ *_ coding:utf-8 _*_import sysshopping_car = []product_list_title = ' Product list ' welcome = ' welcome to the shopping ' product_list = [ (' iphone ', 3888), (' ThinkPad ', 4888), (' Coffee ', , (' Mac ', 6888)]print welcomesalary = input (' please input Your salary: ') while true: print product_list_title for item in product_list: print Product_list.index (item) +1,item choice = input (' Please input the name of goods: ') if choice > 4 or choice < 0: print ' No such goods,please reselect ' continue elif choice <= 4 and Choice >= 1: if salary < product _list[choice-1][1]: print ' Account balance is insufficient, please buy other products or quit ' continue else: shopping_ Car.append (product_list[choice-1]) #将商品添加到购物车 print ' The goods you had buy ' for goods in shopping_car: print goods[0],goods[1] salary = salary - product_list[choice-1][1] print ' your account balance is %s ' %salary elif choice == 0: print ' your goods is ' for goods in shopping_car: print goods[0], goods[1] print ' Your Balance is ', salary sys.exit (' Program exit!!! ')
Python list Exercises