python制作的食物买卖存量计算程序

功能类似于收银员,实现食物买卖时存量的增减,可查询食物种类列表,单种食物的存量,总览全部食物的存量。

#!/usr/bin/python

#Filename: bscsystem

import pickle

import commands

import os

if not os.path.exists(’list'):

   os.mknod("list")

   print 'mkdir "list"'

if commands.getoutput('wc -l list')=='0 list':

   an={}

   fn=open("list","wb")

   pickle.dump(an,fn)

   fn.close()

f=open("list","rb")

content=pickle.load(f)

f.close()

while True:

   answer=raw_input('show:h  check:c  buy:b  sell:s  all:a  del:d  exit:e \n')

   if answer=='h':

       for key in content.keys():

           print key,

       print

   elif answer=='c':

       answer=raw_input('which food you want to check?\n')

       if content.has_key(answer):

           print 'The number of "%s" is "%s"'%(answer,content[answer])

       else:

           print 'No such food'

   elif answer=='b':

       na=raw_input('which food?\n')

       nu=int(raw_input('how many foods?\n'))

       if content.has_key(na):

           content[na]=content[na]+nu

           print 'The number of "%s" is "%s"'%(answer,content[answer])

       else:

           content[na]=nu

   elif answer=='s':

       na=raw_input('which food?\n')

       nu=int(raw_input('how many foods?\n'))

       if content.has_key(na):

           content[na]=content[na]-nu

           print 'The number of "%s" is "%s"'%(answer,content[answer])

       else:

           print 'no such food\n'

   elif answer=='e':

       break
   elif answer=='a':

       print content

   elif answer=='d':

       na=raw_input('which food you want to delete?\n')

       if content.has_key(na):

            del content[na]

            print '"%s" is deleted'%na

       else:

            print 'no such food'

    else:
        print 'unknow command'

f2=open("list","wb")

pickle.dump(content,f2)

f2.close()


你可能感兴趣的:(phthon食物存量小程序)