简易购物功能实现
2019独角兽企业重金招聘Python工程师标准>>>
#! /usr/bin/env python #coding: utf-8#file: market.py #Author: toddlerya #date: 11-28-2014 #version: 1.0"""" 购物系统功能要求:1、前提必须指定用户登录进去,才可进行购买!2、登录进去之后,提示用户进行充值金额,否则无法进行购买物品!3、充值成功后,提示用户是否进行购买(不购买直接退出系统)4、用户同意购买之后,把所有产品列出供用户选择5、针对之前用户充值的金额来判断用户哪些用户不能买,并给予提示信息;能购买的话,把物品加入购物框6、如果用户输入的产品不存在,也给予相应的提示信息"""objects = ['coffee','car','mobile','Internet','clothes'] prices = [35,30000,3000,6000,500] shop_list = []name = "jojo" passwd = "bean" choose = "" choice = ""def login():while True:name = raw_input("please input your name:")if name == "jojo": password = raw_input("please input your password:")while password != 'bean':password = raw_input("please input your password:")print "----------------------------------"print "welcome to login to system!\n"print "----------------------------------"breakelse:print "+-----------------------------+"print "user %s not found,try again!" % nameprint "+-----------------------------+"continue def recharge():global self_sal self_sal = (raw_input("please store your money: "))while not self_sal.isdigit():self_sal = raw_input('not a number, please try again: ')self_sal = int(self_sal)while True:if self_sal > 0:print '\n--------------------------------------'print 'you had stored %s successfully!\n' % self_salbreakelse :print '---------------------------------'self_sal = int(raw_input("you store money failure,\nplease store money again:"))continuedef shop():purchase = raw_input('Do you want to shop(yes/no):%s' % choose)if purchase == 'yes':for i in objects:print i,'\t',prices[objects.index(i)]elif purchase == 'no':print '\n------------------------------------'print 'you had exited system successfully!'print '----------------------------------'global choicechoice = "!!!"F_choice = choice.strip()login() recharge() shop()while choice != "!!!":choice = raw_input("Please input one item to buy(input '!!!' to quit): ")F_choice = choice.strip()if F_choice == "!!!":breakif F_choice in objects:objects_index = objects.index(F_choice)objects_price = prices[objects_index]print F_choice,'---->',objects_priceif self_sal > objects_price:shop_list.append(F_choice)print 'Added %s into shop list' % choiceself_sal = self_sal - objects_priceprint "Salary left:",self_salchoice = ""continueelse:if self_sal < min(prices):print 'Sorry,rest of your salary cannot buy anything!88'print "you hava bought these things: %s" % shop_listbreakelse:print "Sorry,you cannot afford this product,please try other ones!\n"print "-----------------------------------------------------------------"choice = raw_input("please input one item to buy(input '!!!' to quit):")F_choice = choice.strip()continueelse:print "Not such products!Please input one item to buy again!(input '!!!' to quit):"print "-------------------------------------------------------"choice = raw_input("please input one item to buy(input '!!!' to quit):") F_choice = choice.strip()continue转载于:https://my.oschina.net/u/2003106/blog/350223
总结
- 上一篇: Dialog外部点击事件
- 下一篇: 实验楼项目课学习笔记-jQuery翻转拼