TA的每日心情 | 难过 2024-11-13 00:16 |
---|
签到天数: 370 天 [LV.9]以坛为家II
|
以下是其他用户优化的代码:- #! /usr/bin/env python
- #encoding: 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 "----------------------------------"
- break
- else:
- print "+-----------------------------+"
- print "user %s not found,try again!" % name
- print "+-----------------------------+"
- 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)</font>
-
- while True:
- if self_sal > 0:
- print '\n--------------------------------------'
- print 'you had stored %s successfully!\n' % self_sal
- break
- else :
- print '---------------------------------'
- self_sal = int(raw_input("you store money failure,\nplease store money again:"))
- continue
- def 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 choice
- choice = "!!!"
- 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 == "!!!":
- break
- if F_choice in objects:
- objects_index = objects.index(F_choice)
- objects_price = prices[objects_index]
- print F_choice,'---->',objects_price
- if self_sal > objects_price:
- shop_list.append(F_choice)
- print 'Added %s into shop list' % choice
- self_sal = self_sal - objects_price
- print "Salary left:",self_sal
- choice = ""
- continue
- else:
- if self_sal < min(prices):
- print 'Sorry,rest of your salary cannot buy anything!88'
- print "you hava bought these things: %s" % shop_list
- break
- else:
- 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()
- continue
- else:
- 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
复制代码 |
|