数据库中记录如下:
代码如下:
#!/usr/bin/env python
#coding:utf-8
import MySQLdb
import os
import sys
conn=MySQLdb.connect(user='root',passwd='123456',host='192.168.2.71',db='login')
cur=conn.cursor()
cur_user=conn.cursor()
cur_user.execute("select * from user_account")
cur.execute("select * from lock_account")
user_names=cur_user.fetchall()
lock_names=cur.fetchall()
login_names={}#转换用户名和密码
user_list = []
for name in user_names:
a = name[0]
b = name[1]
user_list.append(a)
login_names[a]=b
lock_users=[] #获取已经锁过的账号
for name in lock_names:
names=name[1]
lock_users.append(names)
retry_counter = 0
while retry_counter < 3 :#循环三次
user = raw_input('Username:').strip()
if len(user) ==0: #账号不为空
print "Username cannot be empty!"
continue
if user in lock_users:
print "username is already locked"
break
passwd = raw_input('Password:').strip()
if user in login_names and passwd==login_names[user]:
print "Welcome %s loging my system" % user
break
else:
retry_counter+=1
else:
cur.execute("insert into lock_account(name) values(%s)",user)
conn.commit()
print lock_users
print "Your are not allowed to login!,now will lock %s account" % user
exit()
def function_list():
while True:
print """\033[;32m\t\t 欢迎使用abc银行自动取款机
请注意保管好您的账号和密码,以免为你的财产造成损失。
有任何疑问请拨打xxx--000-xxx为您服务,丢失不赔偿的哦亲\033[0m\n"""
print "\t(1) 账号信息查询 "
print "\t(2) 刷卡消费"
print "\t(3) 存取款转账"
print "\t(4) 密码修改"
print "\t(5) 取卡退出"
choices = raw_input("This is only 5 item Please input choices:").strip()
if len(choices) == 0:continue
if choices == '1':select_list()
elif choices == '2':shoping()
elif choices == '3':sdtmoney()
elif choices == '4':change_passwd()
elif choices == '5':
print "\033[;31mPlease pay attention to the property security\033[0m"
exit()
else:
continue
def change_passwd():
while True:
ones_pass = raw_input('once time:').strip()
twice_pass = raw_input("once again").strip()
if len(ones_pass)==0 or len(twice_pass)==0:
print "password is not allow empty"
continue
if ones_pass == twice_pass:
cur.execute("update user_account set passwd = %s where card_name=%s",(twice_pass,user))
conn.commit()
print "Your passwd is change when you login again please input new passwd"
break
else:
print "first pass is not equil two pass please input again"
continue
def sdtmoney():
while True:
conn=MySQLdb.connect(user='root',passwd='123456',host='192.168.2.71',db='login')
cur=conn.cursor()
cur_user=conn.cursor()
cur_user.execute("select * from user_account")
user_names=cur_user.fetchall()
user_list = []
for name in user_names:
a = name[0]
b = name[1]
user_list.append(a)
login_names[a]=b
print "\t(1) 存款 "
print "\t(2) 提现"
print "\t(3) 转账"
print "\t(4) 返回主菜单"
choice = raw_input('This only 4 item, Plase input your select choice:').strip()
if choice == '1':
in_money = raw_input('please input you in_money:').strip()
balance=int(in_money)+int(user_names[user_list.index(user)][2])
cur.execute("update user_account set balance = %s, option_type='存款',pay_cash=%s where card_name=%s",(balance,in_money,user))
cur.execute("insert into balan_list(card_name,balance,option_type,pay_cash,option_time) values(%s,%s,%s,%s,now())",(user,balance,choice,in_money))
conn.commit()
elif choice == '2':
out_money = raw_input('please input you out_money:').strip()
balance=int(user_names[user_list.index(user)][2])-int(out_money)*0.05-int(out_money)
cur.execute("update user_account set balance = %s where card_name=%s",(balance,user))
cur.execute("insert into balan_list(card_name,balance,option_type,pay_cash,option_time) values(%s,%s,%s,%s,now())",(user,balance,choice,out_money))
conn.commit()
elif choice =='3':
transfer_name = raw_input('请输入要转账的卡号:').strip()
transfer_money = raw_input('请输入要转账的金额:').strip()
a = int(user_names[user_list.index(user)][2])-int(transfer_money)-int(transfer_money)*0.1
b =int(user_names[user_list.index(transfer_name)][2])+int(transfer_money)
cur.execute("update user_account set balance = %s where card_name=%s",(a,user))
cur.execute("update user_account set balance = %s where card_name=%s",(b,transfer_name))
cur.execute("insert into balan_list(card_name,balance,option_type,pay_cash,option_time) values(%s,%s,%s,%s,now())",(user,a,str(int(choice)-6),int(transfer_money)-int(transfer_money)*2))
cur.execute("insert into balan_list(card_name,balance,option_type,pay_cash,option_time) values(%s,%s,%s,%s,now())",(transfer_name,b,choice,transfer_money))
conn.commit()
else:
function_list()
def select_list():
while True:
print "\t(1) 消费详情"
print "\t(2) 账单查询"
print "\t(3) 返回主菜单"
import MySQLdb.cursors
conn=MySQLdb.connect(user='root',passwd='123456',host='192.168.2.71',db='login',cursorclass = MySQLdb.cursors.DictCursor)
cur=conn.cursor()
cur.execute("select * from hist_list")
cur_user=conn.cursor()
cur_user.execute("select * from balan_list")
balan_list = cur_user.fetchall()
user_names = cur.fetchall()
choice = raw_input('This only 3 item, Plase input your select choice:').strip()
if choice == '1':
print """\033[;32m您的消费记录如下\033[0m\n"""
for card_num in user_names:
if user in card_num['order_user']:
print card_num
elif choice == '2':
print """\033[;32m您的历史账单记录如下\033[0m\n"""
for card_num in balan_list:
if user in card_num['card_name']:
print card_num
else:
function_list()
def shoping():
conn = MySQLdb.connect(host='localhost', user='root',passwd='123456',db='login')
cur=conn.cursor()
cur.execute('select * from order_list')
products =list( cur.fetchall())
print "您当前账号余额是:",user_names[user_list.index(user)][2]
print "\t input you want to buy sths"
salary = int(user_names[user_list.index(user)][2])
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
break
if len(choice) ==0 or not choice.isdigit():
continue
choice = int(choice)
pro = products[choice]
if salary >= pro[2]:
salary = salary - pro[2]
shopping_list.append(pro)
cur.execute("update user_account set balance = %s where card_name=%s",(salary,user))
cur.execute("insert into hist_list(order_num,order_name,order_price,order_user,order_time) values(%s,%s,%s,%s,now())",(pro[0],pro[1],pro[2],user))
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 backup functlist' % (pro[1],pro[2],salary)
break
if __name__ == "__main__":
function_list()
部分效果图: