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

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

思维导图;

#!/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购物车----运维开发初学_第1张图片 

执行效果:

python购物车----运维开发初学_第2张图片

你可能感兴趣的:(python购物车----运维开发初学)