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

python购物车----运维开发初学

2016-02-28 18:34 681 查看
摘要: 需求:输入工资,输出购物菜单及价格,计算是否可支付,输出用户剩余的钱,问是否继续,直到钱不够

需求:输入工资,输出购物菜单及价格,计算是否可支付,输出用户剩余的钱,问是否继续,直到钱不够

思维导图;



#!/usr/bin/env python
#coding:utf-8
import sys
import MySQLdb
conn = MySQLdb.connect(host='localhost', user='root',passwd='',db='mylog')
cur=conn.cursor()
cur.execute('select * from shop_list')

products =list( cur.fetchall())
salary = int(raw_input('please input you salary:'))
shopping_list = [] #购物列表
while True:
for p in products:
print p[0],p[1],p[2] #打印菜单列表
choice = raw_input("\033[32;1mplease choose sth to buy:\033[0m").strip()
if choice=='quit':
print 'you have bought below statf:'
for i in shopping_list:
print '\t',i
sys.exit('goodbay!')
if len(choice) ==0 or not choice.isdigit():
continue
choice = int(choice)
pro = products[choice]
print pro
if salary >= pro[2]:
salary = salary - pro[2]
shopping_list.append(pro)
cur.execute("insert into hist_list(order_num,order_name,hist_price,buy_time) values(%s,%s,%s,now())",pro)
conn.commit()#保存购物列表和购物时间到数据库
print "adding %s to shoping list,you have %d left" % (pro[1],salary)
else:
print 'the price of %s is %s,yet your salary currnet balance is %s ,so try another' % (pro[1],pro[2],salary)

数据库设计;





执行效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: