模块购物商城和ATM机代码:

http://outofmemory.cn/python/video/let-us-python/ python为程序员服务


 快来加入群【python爬虫交流群】(群号570070796),发现精彩内容。


一下三个模块完美的实现了模块之间的参数传递。
1、将编写好的代码设置成模块
2、设置成模块之后还存在一些导入模块之后,怎么时候执行,怎么执行的问题
这个问题使用如下代码解决。  
if __name__=='__main__'#如果程序是主函数自己执行的,(意思就是该函数不是被别人import(被调用)执行的),那么久执行shop()函数,你就可以先导入到你的当前代码中,导入的方法为(import _shopping),此时导入进来的模块将不会执行shop(),你需要调用执行的时候,你只需输入(  _shopping.shop()  )代码,即可调用执行。
_shopping.shop()
else:                        
shop()
if __name__=='__main__':
bank.bank(gouwuche,shopping_userlogin,shangpinname)
else:
bank(None,None,None)
3、模块之间的参数如何传递系列的问题会呈现出来;

def bank(gouwuche,shopping_userlogin,shangpinname):

5、如何将传递的参数写入日志,并且查询:
        使用pickle或者是json,
 
main模块

#coding=utf-8
import sys
import _shopping
import bank
print (u'购物请按1,银行业务请按2,退出请按任意键:')
choise=raw_input(u'------->>>').strip()
if choise=='1':
_shopping.shop()
elif choise=='2':
bank.bank(None,None,None)
else:
print (u'您选择了退出操作,欢迎再次来到彼岸:')
sys.exit()

shopping模块
#coding=utf-8
#__author__ = 'Administrator'
import bank
def shop():
import sys,pickle,time
user={'haha':'123',
'huaqiangu':'123',
'baizihua':'123',
'123':'123',
'321':'123'
}
gouwuche=[]
shangpinname={'iphon6':4980,'xiaomi':999,'huawei':2999,'chongdianbao':59,'shujuxian':15}
count=0
def look_gouwuche():
print u'----------已购买的商品为----------:'
for k in gouwuche:
print '----------|',k,':',gouwuche[k],'|----------'
def after_choise():
chose11=raw_input(u'立即购买请按1,加入购物车请按2:')
if chose11=='1':
print 'tiao zhuan dao yinhang!'
def dump_shoppinguser():
f=open('H:\\Python\\shopping_bank\\shopping_user.txt','w')
pickle.dump(user,f)
f.close()
def load_shoppinguser():
f=open('H:\\Python\\shopping_bank\\shopping_user.txt')
a=pickle.load(f)
print a
f.close()
def add_gouwuche(dict,gouwuche):
a=0
for i in gouwuche:
a=a+dict[i]
return a
def display_gouwuche(dict,gouwuche):
print '-----------------------------------'
for huo in gouwuche:
print '----|',huo,':',dict[huo]
print '-----------------------------------'
def del_gouwuche(dict,gouwuche):
while True:
if len(gouwuche)!=0:
print (u'购物车列表为:')
display_gouwuche(dict,gouwuche)
print u'总额为:',add_gouwuche(dict,gouwuche)
del_name=raw_input(u'请输入要删除的商品名称:').strip()
for i in gouwuche:
if i==del_name:
gouwuche.remove(del_name)
print u'删除成功!'
print '----------------------------------------------------'
print u'剩余商品为:',display_gouwuche(shangpinname,gouwuche)
print u'总额为',add_gouwuche(shangpinname,gouwuche)
print '----------------------------------------------------'
break
choise=raw_input(u'继续删除商品请按1,结算请按2,继续购物请按3,退出请按任意键').strip()
if choise=='1':
continue
elif choise=='2':
goto_bank(gouwuche,shopping_userlogin,shangpinname)
break
elif choise=='3':
break
else:
sys.exit()
else:
print (u'购物车为空:')
break
def shopping_loger(shopping_user,gouwuche,accountdict,payaccount,money,number):
f=open('H:\\Python\\shopping_bank\\shopping_loger.txt','a')
for name in gouwuche:
msg='%s %s %s %s %s %s %s %s' %(time.ctime(),shopping_user,payaccount,accountdict[payaccount][1],gouwuche[name],money,'shouxufei',number)
f.write(msg+'\n')
f.close()
def goto_bank(gouwuche,shopping_user,shangpingname):
bank.bank(gouwuche,shopping_user,shangpingname)
while True:
haha=''
f=open('H:\\Python\\shopping_bank\\shopping_user.txt')
a=pickle.load(f)
f.close()
print (u'--------------欢迎来到彼岸商城,祝您购物愉快!!!-------')
shopping_userlogin=raw_input(u'————输入账号:').strip()
# print u'再次判断',a
if a.has_key(shopping_userlogin):
f=open('H:\\Python\\shopping_bank\\lock_user.txt')
for name in f.xreadlines():
name=name.strip().split()
if name[0]==shopping_userlogin:
print (u'您的账号已经被锁定,请及时联系华神!')
haha='yi_suo_zhu'
break
f.close()
if haha=='yi_suo_zhu': #如果账户已锁定返回主界面
continue
else:pass
password=raw_input(u'请输入密码:').strip()
if password==a[shopping_userlogin]:
print (u'---登入成功,祝您购物愉快!---')
print '----------------------------------------------------'
for huo in shangpinname:
print '--------|',huo,':',shangpinname[huo],'-------'
print '----------------------------------------------------'
while True:
commodity_name=raw_input(u'_---继续购买请输入商品名称,查看购物车并做其他操作请按任意键:').strip()
if shangpinname.has_key(commodity_name):
chose11=raw_input(u'立即购买请按1,加入购物车请按2,查看购物车请按3:').strip()
if chose11=='1':
gouwuche.append(commodity_name)
goto_bank(gouwuche,shopping_userlogin,shangpinname)
elif chose11=='2': #加入购物车
gouwuche.append(commodity_name)
print (u'添加购物车成功!')
display_gouwuche(shangpinname,gouwuche)
elif chose11=='3':
display_gouwuche(shangpinname,gouwuche)
choise3=raw_input(u'立即购买请按1,删除购物车商品请按2:').strip()
if choise3=='1':
goto_bank(gouwuche,shopping_userlogin,shangpinname)
if choise3=='2':
del_gouwuche(shangpinname,gouwuche)
else:
print (u"输入错误,退回主界面!")
else:
print (u'选项错误!')
else:
display_gouwuche(shangpinname,gouwuche)
choise=raw_input(u'立即购买请按1,删除购物车商品请按2,返回上一级请按任意键:').strip()
if choise=='1':
goto_bank(gouwuche,shopping_userlogin,shangpinname)
if choise=='2':
del_gouwuche(shangpinname,gouwuche)
else:
print (u"输入错误,退回主界面!")
else:
if count<=2:
count+=1
print u'密码错误!---温馨提示:还有%d次输入不正确账户将被锁定。'%(3-count)
continue
elif count==2:
print u'您的账户已经被锁定:'
f=open('H:\\Python\\shopping_bank\\lock_user.txt','a')
f.write(userlogin+'\n')
f.close()
print u'三次登入失败,您的账户已经被锁定!'
continue
else:
print(u'用户未注册!')
choise=raw_input(u'用户注册请按1,继续请按任意键:')
print 'haha'
if __name__=='__main__':
_shopping.shop()
else:
shop()

银行ATM机模块

#coding=utf-8
#__author__ = 'Administrator'
import _shopping
def bank(gouwuche,shopping_userlogin,shangpinname):
import time
import pickle,sys
from getpass import getpass
def find(dict,userlogin):
print (u'''-------------------------------------------------
用户名:%s
余 额:%d'''%(dict[userlogin][1],dict[userlogin][0]))
choise=raw_input(u'请输入查找的年月如(2015Jun)将会查询2015年六月所有相关的账户日志:').strip()
if len(choise)==len('2015Jun'):
choise0=choise[:4]
choise1=choise[4:]
print choise0,':',choise1
f=open('H:\\Python\\shopping_bank\\bank_loger.txt','r')
for line in f.xreadlines():
line=line.strip().split()
if line[5]==userlogin and line[4]==choise0 and line[1]==choise1:
print '--|',line[4],line[1],line[2],line[0],line[3],u'您的操作类型为:',line[5:],'|--'
else:print (u'查无此日志,请输入其他年份和月份!')
f.close()
def come_or_out(choise0):
while True:
if choise0=='1':
sys.exit()
elif choise0=='2':
break
else:
print (u'输入错误,请重新输入!')
break
def come_or_out7(choise0):
while True:
if choise0=='1':
sys.exit()
elif choise0=='2':
break
elif choise0=='3':
_shopping.shop()
else:
print (u'输入错误,请重新输入!')
break
def panduan_zhuanghu(username,):
f=open('H:\\Python\\shopping_bank\\lock_account.txt','r')
for i in f.xreadlines():
i=i.strip().split()
if username==i[0]:
print (u'账户已经被锁定,请赶紧拨打15595569905请 幻神 吃饭去,记得一定要吃好的!')
sys.exit()
def panduan_input(money):
while True:
try:
money=int(raw_input(u'请输入金额:'))
break
except ValueError:
print (u'请输入一个数字!')
def take(dict,userlogin):
while True:
try:
money=int(raw_input(u'请输入金额:').strip())
break
except ValueError:
print (u'请输入一个数字!')
if money>dict[userlogin][0]:
print (u'账户余额不足!')
elif money<=dict[userlogin][0]:
dict[userlogin][0]=dict[userlogin][0]-(money+money*0.05)
f=open('H:\\Python\\shopping_bank\\userlist.txt','wb')
pickle.dump(dict,f)
f.close()
bank_loger(userlogin,dict[userlogin][1],'Take',money,money*0.05,'1')
print u'钞票太多,正在为您使劲吐出钞票..、。。。.................\n-------取款成功。'
def save(dict,uselogin):
while True:
try:
money=int(raw_input(u'存取的金额为:'))
break
except ValueError:
print (u'输入错误,请输入数字')
dict[userlogin][0]=dict[userlogin][0]+money
f=open('H:\\Python\\shopping_bank\\userlist.txt','wb')
pickle.dump(dict,f)
f.close()
bank_loger(userlogin,dict[userlogin][1],'save',money,'0','1')
print (u'存款成功')
def repayment(dict,userlogin):
while True:
try:
money=int(raw_input(u'请输入金额:'))
break
except ValueError:
print (u'请输入一个数字!')
bank_loger(userlogin,dict[userlogin][1],'repament',money,'0','1')
print (u'还款成功!')
def transfer(dict,userlogin):
while True:
another_account=raw_input(u'请输入对方账号:').strip()
retype=raw_input(u'请再输入一次:')
if another_account==retype:
if dict.has_key(retype):
print (u'要转入的账号名称为:%s'%dict[retype][1])
choise1=raw_input(u'继续请按1,返回请按任意键:').strip()
if choise1=='1':
while True:
try:
tran=int(raw_input(u'请输入转账金额:'))
break
except ValueError:
print (u'输入错误!请输入数字!')
if tran<=dict[userlogin][0]:
dict[userlogin][0]=dict[userlogin][0]-(tran+tran*0.05)
dict[retype][0]=tran+dict[retype][0]
f=open('H:\\Python\\shopping_bank\\userlist.txt','wb')
pickle.dump(dict,f)
f.close()
bank_loger(userlogin,dict[userlogin][1],'transfer_to_%s'%retype,tran,(tran*0.05),'1')
bank_loger(retype,dict[retype][1],'transfer_form_%s'%userlogin,tran,'0','1')
print (u'转账成功!手续费刚刚为%f元哦!'%(tran*0.05))
break
else:
print (u'账户余额不足!')
break
else:
break
else:
print(u'查无此账户!一般情况下我是不会出现的,看来这回情况不一般呀!')
break
else:
print u'两次账号不相同,请重新输入!'
continue
def bank_loger(account,accountname,tran_type,amount,shouxufei,number):
f=open('H:\\Python\\shopping_bank\\bank_loger.txt','a')
msg='%s %s %s %s %s %s %s' %(time.ctime(),account,accountname,tran_type,amount,shouxufei,number)
f.write(msg+'\n')
f.close()
# bank.bank(gouwuche,shopping_userlogin,shangpinname)
def shopping_loger(shopping_user,gouwuche,accountdict,payaccount,money,number):
f=open('H:\\Python\\shopping_bank\\shopping_loger.txt','a')
for name in gouwuche:
msg='%s %s %s %s %s %s %s %s' %(time.ctime(),shopping_user,payaccount,accountdict[payaccount][1],gouwuche[name],money,'shouxufei',number)
f.write(msg+'\n')
f.close()
def display_gouwuche(dict,gouwuche):
print '-----------------------------------'
for huo in gouwuche:
print '----|',huo,':',dict[huo]
print '-----------------------------------'
def add_gouwuche(dict,gouwuche):
a=0
for i in gouwuche:
a=a+dict[i]
return a
def buy(shangpinname,gouwuche,account_dict,userlogin,shopping_userlogin):
a=add_gouwuche(shangpinname,gouwuche)
account_dict[userlogin][0]=account_dict[userlogin][0]-a
f=open('H:\\Python\\shopping_bank\\userlist.txt','wb')
pickle.dump(account_dict,f)
f.close()
for i in gouwuche:
bank_loger(userlogin,account_dict[userlogin][1],'shopping_%s'%i,shangpinname[i],shopping_userlogin,'1')
print (u'购买成功!')

def goto_pay(shopping_userlogin,shangpinname,gouwuche,account_dict,account_login):
if len(gouwuche)!=0:
print u'-----------------购物车中的商品为:'
display_gouwuche(shangpinname,gouwuche)
print u'总金额为:',add_gouwuche(shangpinname,gouwuche)
choise=raw_input(u'购买请按1,退出请按任意键:').strip()
if choise=='1':
buy(shangpinname,gouwuche,account_dict,userlogin,shopping_userlogin)
else:
sys.exit()
else:
print u'购物车为空!'
while True:
f=open('H:\\Python\\shopping_bank\\userlist.txt')
a=pickle.load(f)
print a
f.close()
print u'----------------欢迎使用华神ATM机!-----------------'
userlogin=raw_input(u'-----请输入如账户名:').strip()
if a.has_key(userlogin):
panduan_zhuanghu(userlogin) #判断用户是否被锁定
count=0
while True:
f=open('H:\\Python\\shopping_bank\\userlist.txt')
a=pickle.load(f)
f.close()
password=raw_input(u'--------请输入密码:').strip()
if a[userlogin][2]==password:
print 'username is ',a[userlogin][1]
pass
else:
count+=1
if count<3:
print u'密码错误!温馨提示,还有%d次输入不正确账户将被锁定。'%(3-count)
else:
f=open('H:\\Python\\shopping_bank\\lock_account.txt','a')
f.write(userlogin+'\n')
f.close()
print (u'账户%s已被锁定'%userlogin)
sys.exit()
while True:
bank_choise=raw_input(u'''----------------请选择您要操作的业务----------------------
1、取现 2、查询 3、还款 4、转账 5、存款 6、退出 7、购物支付
----------------------------------------------------------
>>>:''').strip()
if bank_choise=='1':
take(a,userlogin)
come_orout=raw_input(u'退出请按1,返回上一级请按2:').strip()
come_or_out(come_orout)
elif bank_choise=='2':
find(a,userlogin)
come_orout=raw_input(u'退出请按1,返回上一级请按2:').strip()
come_or_out(come_orout)
elif bank_choise=='3':
repayment(a,userlogin)
come_orout=raw_input(u'退出请按1,返回上一级请按2:').strip()
come_or_out(come_orout)
elif bank_choise=='4':
transfer(a,userlogin)
come_orout=raw_input(u'退出请按1,返回上一级请按2:').strip()
come_or_out(come_orout)
elif bank_choise=='5':
save(a,userlogin)
come_orout=raw_input(u'退出请按1,返回上一级请按2:').strip()
come_or_out(come_orout)
elif bank_choise=='6':
sys.exit()
elif bank_choise=='7':
print u'彼岸商城登入的账户为:%s'%shopping_userlogin
goto_pay(shopping_userlogin,shangpinname,gouwuche,a,userlogin)
come_orout=raw_input(u'退出请按1,返回上一级请按2,购物请按3:').strip()
come_or_out7(come_orout)
else:
print (u'输入错误,本次操作即将退出!')
sys.exit()
if __name__=='__main__':
bank.bank(gouwuche,shopping_userlogin,shangpinname)
else:
bank(None,None,None)

 

posted @ 2015-07-13 10:30  你居然是华神  阅读(633)  评论(0编辑  收藏  举报