Python列表练习
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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列表练习的主要内容,如果未能解决你的问题,请参考以下文章