您的位置:首页 > 编程语言 > Python开发

python---购物扩展

2016-01-13 18:43 651 查看
创建一个商品页面,要求
1、显示商品名称和价格对应关系

2、获取用户工资水平
3、提供用户选择购买商品功能
4、用户购买完商品,显示用户账号余额
5、提示用户是否继续购买
#!/usr/bin/env python     //程序开始
#File: list.py
#Date: 2016-01-13

import sys      //插入模块,方便调用函数

Goods = ['Iphone6','QueCao','LV','Car']     //定义商品和价格列表内容
Prices = [5200,80,750,30000]
lists = []             //定义空列表,类似购物车

wide = 50
title = 'shoplist'
half = (50 - len(title) - 2) // 2     //打印标题使用的变量

while True:      //异常处理,要求输入工资必须是整数
try:
wage = int(raw_input("Hello, please input your wages: "))
break
except ValueError:
print "Please input a number, not string."
while True:
print "Look the shoplist, please input what you want:"
print '=' * 50
print '*' * half + ' ' + title + ' ' + '*' * half
print '%-25s%25s' %('Goods','Prices')
print '-' * 50
for p in Goods:
print "%-25s%25s" %(p,Prices[Goods.index(p)])
print '=' * 50                          //显示菜单内容
shop = raw_input("Please input one item to buy:") .strip()
if shop == 'quit':     //退出选项
print "You have bought these things: %s" % lists
sys.exit()
elif shop in Goods:     //商品在列表中时
pay = Prices[Goods.index(shop)]      //取出价格
print "%s, %d" %(shop,Prices[Goods.index(shop)])
if wage > pay:        //当工资大于价格时
wage -= pay          //购买后的余额
lists.append(shop)        //将购买商品添加到空列表
print "OK, %s put in list. Your wage now left: %d" % (shop,wage)
else:
if wage < min(Prices):      //工资大于最小价格
print "Your money can't pay for anything. You have bought these things: %s" % lists
sys.exit()     //退出页面
else:
print "Sorry, this %s you can't put it, please look other!" % shop
else:
print "Your choice not in Goods!"
break
注:python2.7,练习使用!

本文出自 “经验来自痛苦” 博客,谢绝转载!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: