python语言编写的购物系统-定位原创
购物系统功能要求:1、前提必须指定用户登录进去,才可进行购买!
2、登录进去之后,提示用户进行充值金额,否则无法进行购买物品!
3、充值成功后,提示用户是否进行购买(不购买直接退出系统)
4、用户同意购买之后,把所有产品列出供用户选择
5、针对之前用户充值的金额来判断用户哪些用户不能买,并给予提示信息;能购买的话,把物品加入购物框
6、如果用户输入的产品不存在,也给予相应的提示信息
实现功能代码:
#!/usr/bin/python
objects = ['coffee','car','mobile','Internet','clothes']
prices =
shop_list = []
while True:
name = raw_input("please input your name:")
if name == 'dingwei':
password = raw_input("please input your password:")
while password != 'cbh':
password = raw_input("please input your password:")
print "----------------------------------"
print "welcome to login to system!"
print "------------------------------------"
else:
print "user %s not found,try again!" % name
continue
self_sal = int(raw_input("please store your money:"))
while True:
if self_sal > 0:
print '--------------------------------------------'
print 'you had stored %s successfully!' % self_sal
break
elif self_sal == 0:
self_sal = int(raw_input("you store your money failure ,please store money again:"))
continue
else:
self_sal = int(raw_input("you store your money failure ,please store money again:"))
continue
while True:
choose = ''
purchase = raw_input('Do you want to shop(yes/no):%s' % choose)
if purchase == 'yes':
for i in objects:
print i,' ',prices
elif purchase == 'no':
print '------------------------------------'
print 'you had exited system successfully!'
break
else:
continue
choice = raw_input('Please input one item to buy:')
F_choice = choice.strip()
while True:
if F_choice in objects:
objects_index = objects.index(F_choice)
objects_price = prices
print F_choice,objects_price
if self_sal > objects_price:
shop_list.append(F_choice)
print 'Added %s into shop list' % F_choice
self_sal = self_sal - objects_price
print "Salary left:",self_sal
break
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!"
print "-----------------------------------------------------------------"
choice = raw_input('please input one item to buy:')
F_choice = choice.strip()
continue
else:
print "Not such products!Please input one item to buy again!"
print "-----------------------------------------------------------------"
choice = raw_input('please input one item to buy:')
F_choice = choice.strip()
continue
break
效果截图:
此购物系统还存在很多的Bug:
1、此购物系统一次只能买一件物品,要求:一次可以买多件物品。
2、当充值金额的时候,输入非数字的其它字符,会有异常(类型不匹配),会终结程序。要求:对此类异常进行处理,给予提示信息
程序需要进一步完善:
1、物品是我直接在程序里面定义好的,要求:把物品信息单独放在一个文件里面,比如:products.txt,然后通过这个程序来调用products.txt来获取物品信息
第一点我已经解决,如果你还有其它方法的话,也可以。
实现功能代码:
首先在当前路径下创建一个products.txt文件,把物品信息填写进去。
把这段代码放在最上面,然后把上面的两行代码删除掉,代码如下:
objects = ['coffee','car','mobile','Internet','clothes']
prices =
实现功能代码:
f = file('products.txt')
products = []
prices = []
for line in f.readlines():
new_line = line.split()
products.append(new_line)
prices.append(new_line)
2、代码繁琐,重复太多,简化代码。
以下是其他用户优化的代码:#! /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 =
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
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
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
页:
[1]