markdown

alex视频学习作业

需求:从数据库获取商品菜单,可供用户选购

cat db_list.py

#!/usr/bin/env python

import MySQLdb as mysql
import sys
#list_product=[]
list_product1=[]
list_product2=[]
reload(sys)
sys.setdefaultencoding('utf-8')
zabbixip='127.0.0.1'
conn=mysql.connect(host=zabbixip,port=3306,user='root',passwd='123123',db='test',charset='utf8')
cursor=conn.cursor()
#cursor.execute("select * from test.test")
#result=cursor.fetchall()
#len_list=len(result)
cursor.execute('select name from test.test')
result1=cursor.fetchall()
cursor.execute('select value from test.test')
result2=cursor.fetchall()
len_list1=len(result1)

#def list_productf():
#   for i in  range(len_list):
#       for j in range(1,len(result[i])): 
#           list_product.append(result[i][j].encode("ascii"))
#   return list_product
def list_productf():
    for i in range(len_list1):
        list_product1.append(result1[i][0].encode("ascii"))
        list_product2.append(int(result2[i][0].encode("ascii")))
        #print result1[i][0]
    return list_product1,list_product2

cat shop.py

#!/usr/bin/env python
import sys
import db_list
products=[]
prize=[]
#list_product_all=db_list.list_productf()
products,prize=db_list.list_productf()
#print list_product_all
#for i in range(0,2*db_list.len_list,2):
#        products.append(list_product_all[i])
#for i in range(1,2*db_list.len_list,2):
#        prize.append(int(list_product_all[i]))
#products=['car','bag','eggs','iphone','xiaomi','hp','pan' ]
#prize=[100000,20000,10,5888,899,4000,5]
shop_list=[]
shop_list_prize=[]
def all():
    print "\033[33m follow are  all products,please choose one to buy~ \033[0m"
    for i in products:
        print "\033[35m \t%s\t$%d \033[0m" %(i,prize[products.index(i)])

def shoplist():
    print "\033[34m you have bought these things: \033[0m" 
    for i in shop_list:
        print "\033[35m \t\t%s\t$%d \033[0m" %(i,shop_list_prize[shop_list.index(i)])
    
while True:
    try:
        salary=int(raw_input("please input you salary: $ "))
        break
    except ValueError:
        print "\033[33m please input a int number,not string,thanks! \033[0m"

while True:
    choice= raw_input("\033[33m please input name to buy~or (all/shoplist/exit) \033[0m ")
    if choice == "exit":
        shoplist()
        sys.exit()
    if choice == "all":
        all()
    if choice == "shoplist":
        shoplist()
    if choice in products:
        choice_prod_index=products.index(choice)
        choice_prize=prize[choice_prod_index]
        if salary >= choice_prize:
            shop_list.append(choice)    
            shop_list_prize.append(choice_prize)
            salary=salary-choice_prize
            shoplist()
            print "\033[36m your salary left: $ %d\033[0m" %salary
        else:
            if salary < min(prize):
                print "\033[38m sorry,rest of your salary can't buy anything ~\033[0m"
                shoplist()
                sys.exit()
            else:
                print "\033[38m sorry,you can't afford this product,please choose other ones! \033[0m"

你可能感兴趣的:(markdown)