python 购物车程序

python学习网 2017-08-04 15:26:01
用户登录和用户新增
#coding:utf-8
with open('bank_card', 'w', encoding='utf-8') as write_f:
write_f.write("qianmeng 123456 15000\n")
write_f.write("zhangsan 123 15000\n")
write_f.write("lisi abc 15000\n")
def login(name,password):
with open('bank_card','r',encoding='utf-8') as read_f:
for i in read_f:
if i.split()[0]==name and i.split()[1]==password:
print('登录成功')
return 1
else:
print("用户名或密码错误")
return 0
def create_user():
#print('请输入用户名:')
name=input("请输入用户名:")
password=input("请输入密码:")
with open('bank_card','a',encoding='utf-8') as write_f:
a=name+' '+password+' 15000'
write_f.write(a)
return 1

购物程序
#coding:utf-8
goods=[['1','电脑',5000],['2','鼠标',20],['3','手机',3000]]
def buy_goods(id,num):
a=[]
for b in goods:
if b[0]==id:
sale=int(b[2])*num
c=[b[1],num,sale]
a.append(c)
return sale
else:
print("没有此商品")
return 0

银行卡参数说明:
#coding:utf-8
import os
import time
# with open('bank_card', 'w', encoding='utf-8') as write_f:
# write_f.write("qianmeng 123456 15000 1\n")
# write_f.write("zhangsan 123 15000 1\n")
# write_f.write("lisi abc 15000 1\n")
def bank_consumer(name,expence,log_name):
time1=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
expence1=str(expence)
with open('bank_card','r',encoding='utf-8') as read_f,open('.bank_card.swap','w',encoding='utf-8')as write_f:
read_f.seek(0,0)
for i in read_f:
j=i.split()[0]
if name==j:
k=float(i.split()[2])
h=k-expence
a=str(h)
i=i.replace(i.split()[2],a)
with open(log_name, "a", encoding='utf-8') as log_f:
log_f.write(time1)
log_f.write(" 支出 ")
log_f.write(expence1)
log_f.write(" 元 余额为 ")
log_f.write(a)
log_f.write(" 元\n")
write_f.write(i)
os.remove('bank_card')
os.rename('.bank_card.swap', 'bank_card')
return a
#bank_consumer('qianmeng',1000,'qianmeng.log')
def yue(name):
with open('bank_card','r',encoding='utf-8') as read_f:
for a in read_f:
if a.split()[0]==name:
return a.split()[2]
else:
return 0
def tixian(name,num):
d=yue(name)
a=float(d)
shouxu=num*0.05
e=a-num-shouxu
c=str(e)
if e < -20000:
return 0
with open('.bank_card.swap','w',encoding='utf-8')as write_f,open('bank_card','r',encoding='utf-8')as read_f:
for b in read_f:
if b.split()[0]==name:
# e=a-num-shouxu
# c=str(e)
# if e<-20000:
# return 0
m=b.split()[2]
b=b.replace(m,c)
write_f.write(b)
os.remove('bank_card')
os.rename('.bank_card.swap', 'bank_card')
return 1
# a=yue("zhangsan")
# print(a)
# b=tixian('zhangsan',40000)
# print(b)
def zhuanzhang(name,toname,num):
a=yue(name)
b=float(a)
e = b - num
f = str(e)
if e < 0:
return 0
i=yue(toname)
j=float(i)
with open('.bank_card.swap', 'w', encoding='utf-8')as write_f, open('bank_card', 'r', encoding='utf-8')as read_f:
for c in read_f:
if c.split()[0]==name:
# e=b-num
# f=str(e)
# if e<0 :
# return 0
m=c.split()[2]
c=c.replace(m,f)
write_f.write(c)
os.remove('bank_card')
os.rename('.bank_card.swap', 'bank_card')
with open('.bank_card.swap', 'w', encoding='utf-8')as write_f, open('bank_card', 'r',encoding='utf-8')as read_f:
for d in read_f:
if d.split()[0]==toname:
g=j+num
h=str(g)
n=d.split()[2]
d=d.replace(n,h)
write_f.write(d)
else:
print('没有此用户')
return 0
os.remove('bank_card')
os.rename('.bank_card.swap', 'bank_card')
return 1
#zhuanzhang('qianmeng','zhangsan',1000)
def huankuan(name,num):
a=float(yue(name))
if a>0:
return 0
with open('.bank_card.swap', 'w', encoding='utf-8')as write_f, open('bank_card', 'r', encoding='utf-8')as read_f:
for a in read_f:
if a.split()[0]==name:
b=a.split()[2]
c=float(b)
# if c>0:
# os.remove('.bank_card.swap')
# return 0
c=c+num
d=str(c)
a=a.replace(a.split()[2],d)
write_f.write(a)
os.remove('bank_card')
os.rename('.bank_card.swap', 'bank_card')
return 1

主函数,用来运行使用的函数:
#coding:utf-8
import logging
import conf.login
import conf.atm
import conf.gouwuche
#a='1.log'
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
filename='./logs/myapp.log',
#filename=a,
filemode='w')

#logging.debug('This is debug message')
name=input("用户名:")
password=input("密码:")
a=conf.login.login(name,password)
if a==1:
while True:
b=input("请选择需要的操作:1.购物 2.银行操作:")
if b=='1':
print('商品id', '商品名称', '商品价格')
for c in conf.gouwuche.goods:
print(c)
id=input("请输入商品id:")
num1=input("请输入商品数量:")
num=int(num1)
sale1=conf.gouwuche.buy_goods(id,num)
sale=float(sale1)
conf.atm.bank_consumer(name,sale,name+".log")
elif b=='2':
d=input("请选择需要的操作:1.查询余额 2.提现 3.转账 4.还款:")
if d=='1':
e=conf.atm.yue(name)
print('余额为',e)
elif d=='2':
xian=input('请输入金额:')
if xian.isnumeric():
jin=float(xian)
m=conf.atm.tixian(name,jin)
if m==1:
print("提现成功")
continue
else:
print('额度最高为20000')
break
else:
print('格式错误')
break
elif d=='3':
toname=input("请输入被转账的用户名:")
jine=input("请输入转账金额")
if jine.isnumeric():
jine1=float(jine)
k=conf.atm.zhuanzhang(name,toname,jine1)
if k==1:
print('转账成功')
continue
else:
print('余额不足')
break
else:
print('格式错误')
break
elif d=='4':
huankuan_jine=input('请输入还款金额:')
if huankuan_jine.isnumeric():
huankuan_jine1=float(huankuan_jine)
j=conf.atm.huankuan(name,huankuan_jine1)
if j==1:
print('还款成功')
continue
else:
print('账户未欠费,无需还款')
continue
else:
print('没有此选项')
break
else:
print('没有此选项')
break
else:
v=input('是否需要新增用户?(y/n)')
if v=='y':
conf.login.create_user()
print('成功')
else:
print('已退出')








阅读(840) 评论(0)