python练习题:实现购物车功能
2017-04-24 12:23
627 查看
功能要求:
功能挺简单,就是涉及到列表的增加和删除,还有一些逻辑的判断处理。
运行结果如下:
要求用户输入总资产,例如:2000 显示商品列表,让用户根据序号选择商品,加入购物车 购买,如果商品总额大于总资产,提示账户余额不足,否则,购买成功。 附加:可充值、某商品移除购物车
#!/usr/bin/env python # -*- coding:utf-8 -*- shopping_list = [ ("Iphone", 5000), ("Delicious food", 48), ("Mac book", 9800), ("Huawei", 4800), ("Alex python", 32), ("coffee", 24) ] shopping_cart = [] salary = raw_input('please input salary: ') if not salary.isdigit(): print "salary must be digit,run again" exit() else: salary = int(salary) while True: print "------products list is--------" for index, item in enumerate(shopping_list): print "\033[32m%s, %s\033[0m" %(index, item) choice = raw_input('please input choice[q(uit)]>>> ') if choice.isdigit(): choice = int(choice) if choice < len(shopping_list) and choice >= 0: product = shopping_list[choice] if salary > product[1]: confirm = raw_input('do you want to buy now[y/n]: ') if confirm == 'y': shopping_cart.append(product) salary -= product[1] print "you bought %s,price is %d, your balance is %d" % (product[0], product[1], salary) else: print 'select again' else: add_confirm = raw_input("your balance is: %d, not enough, do you want to add more?[y/n]" % salary) if add_confirm == 'y': add_salary = raw_input('add the money: ') if add_salary.isdigit(): add_salary = int(add_salary) salary += add_salary print "now balance is %d: " % salary else: print "the money must be digit." else: print "------shopping cart list---------: " for index, item in enumerate(shopping_cart): print index, item else: print "choice must be 0~5." elif choice == 'q': remove_product = raw_input("do you want remove product or exits now [y/n] ") if remove_product == "y": print "-----------your shopping cart lists-------------: " for index, item in enumerate(shopping_cart): print index, item remove_choice = raw_input('please input your remove choice>>> ') if remove_choice.isdigit() and int(remove_choice) < len(shopping_cart) and int(remove_choice) >= 0: salary += shopping_cart[int(remove_choice)][1] del shopping_cart[int(remove_choice)] print "-----------new shopping cart lists-------------: " for index, item in enumerate(shopping_cart): print index, item print "your balance is %d" % salary else: print "input error, again" else: print "exit now" exit() else: print "-----------shopping cart lists-------------: " for index, item in enumerate(shopping_cart): print index, item print "\033[31mchoice must be digit,exit\033[0m"
功能挺简单,就是涉及到列表的增加和删除,还有一些逻辑的判断处理。
运行结果如下:
相关文章推荐
- Python3实现购物车功能
- Python Django实现简单购物车功能
- 用python实现简单购物车功能
- Python实现购物车功能的方法分析
- 利用python实现简单的循环购物车功能示例代码
- Python实现的购物车功能示例
- python——实现一个购物车的功能
- [转]自定义ASP.NET AJAX拖放功能示例程序:实现IDragSource和IDropTarget接口将商品拖放至购物车中
- python也能实现类初始化方法需求更改的功能
- JSP只用Hashtable和session实现购物车功能的简单实例
- 用Python实现CRUD功能REST服务
- [转] python实现最小功能的 ftp server
- Python实现计算器功能
- 浅谈实现基于Javabean与JSP的购物车功能
- Python实现获取当前公网ip并且自动断开宽带连接功能
- D-Bus入门(四)——QTDBUS代码,实现ofono代码下的python测试文件activite-context的功能
- 使用python代码实现三叉搜索树高效率”自动输入提示”功能
- Python3.2 实现zip压缩与解压缩功能
- 用python 实现上传文件功能
- 使用python实现文本文件合并功能