record-11 程序练习,取款机(数据库)

import pymysql
conn=pymysql.connect(host='192.168.8.30',port=3306,user='root',password='123456',db='hzdl',charset='utf8')
cur=conn.cursor()

while True:
    #提示用户输入账号
    print('请输入账号:')
    #接收用户的输入
    name=input()
    #提示用户输入密码
    print('请输入密码:')
    #接收用户的输入
    password=input()

    #进行账号、密码的校验
    #输出校验结果
    result=cur.execute('select * from user_info where name=%s',name)
    if result==0:
        print("账号不存在")
    else:
        user=cur.fetchone()
        if user[1]!=password:
            print('密码不正确')
        else:
            print('登陆成功')
            break

while True:
    print('请选择服务 1-查询余额 2-取款 3-转账 0-退出')
    choice=input()
    if choice=='1':
        #余额查询时,需要再次从数据库中读取最新的数据
        cur.execute('select * from user_info where name=%s',name)
        user=cur.fetchone()
        print('当前账户余额:%s'%user[2])
    if choice=='2':
        #进行取款交易前,再次从数据库中读取最新的数据
        cur.execute('select * from user_info where name=%s',name)
        user=cur.fetchone()
        print('请输入取款金额:')
        money=float(input())
        if money%50!=0:
            print('金额不合法')
        elif money>1000:
            print('不能超过单笔限额')
        elif money>user[2]:
            print('余额不足')
        else:
            result=user[2]-money
            cur.execute('update user_info set money=%s where name=%s',[result,user[0]])
            conn.commit()
            print('当前账户余额:%s'%result)
    if choice=='3':
        #进行取款交易前,再次从数据库中读取最新的数据
        cur.execute('select * from user_info where name=%s',name)
        user=cur.fetchone()
        print('请输入收款账号:')
        name2=input()
        result2=cur.execute('select * from user_info where name=%s',name2)
        if result2==0:
            print('收款账号不存在')
        else:
            user2=cur.fetchone()
            print('收款账号存在,开始进行转账交易')
            print('请输入转账金额:')
            money2=float(input())
            if money2>2000:
                print('不能超过单笔限额')
            elif money2>user[2]:
                print('余额不足')
            else:
                result3=user[2]-money2
                result4=user2[2]+money2
                cur.execute('update user_info set money=%s where name=%s',[result3,user[0]])
                cur.execute('update user_info set money=%s where name=%s',[result4,user2[0]])
                conn.commit()
                print('当前账户余额:%s'%result3)
    if choice=='0':
        break
            
cur.close()
conn.close()

  

posted @ 2018-01-18 21:28  minkillmax  阅读(133)  评论(0编辑  收藏  举报