标签:python
Python中购物车练习
要求:可以输入工资,选择商品,并且只要用户钱足够就可以一直购买商品,直到余额不足,购买完成后格式化打印用户的余额和商品。
#_*_ coding:utf-8 _*_ import sys shopping_car = [] product_list_title = ‘Product list‘ welcome = ‘Welcome to the shopping‘ product_list = [ (‘iphone‘,3888), (‘thinkpad‘,4888), (‘coffee‘,18), (‘mac‘,6888) ] print welcome salary = 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
原文地址:http://nanchunle.blog.51cto.com/9244770/1913251