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)
数据库设计;
执行效果:
需求:输入工资,输出购物菜单及价格,计算是否可支付,输出用户剩余的钱,问是否继续,直到钱不够
思维导图;
#!/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)
数据库设计;
执行效果:
相关文章推荐
- python 在windows下如何切换工作路径
- python制作报表
- Python计算&绘图——曲线拟合问题(转)
- python遗传算法实现数据拟合(转)
- 详解高速神器python脚步打包android apk,超级快!!(打包系列教程之六)
- 使用Python做科学计算初探(转)
- python 面向对象(进阶篇)
- Python自动化面试必备 之 你真明白装饰器么?
- 面向对象编程其实很简单——Python 面向对象(初级篇)
- python直接执行另一个文件中的代码
- Python用列表生成html表格
- python 继承
- Python 模拟登陆华理教务处
- Python Scarpy安装包
- python 排序
- PCA主成分分析Python实现
- 简要讲解Python编程中线程的创建与锁的使用
- Python中time模块和datetime模块的用法示例
- ubuntu下安装 python 常用软件
- 《Python基础教程》第二章 列表和元组