python简单编程--ATM银行管理系统

由于最近刚接触了python这门编程语言,所以今天就来给大家分享一下如何用python去写一个简单的ATM银行管理系统。

目录

一、ATM功能

  1. 注册
  2. 登录

  3. 查询余额

  4. 存款

  5. 取款

二、完整代码 


一、ATM功能实现代码

1.注册

代码如下:(新用户注册时,系统将奖励3000元)

def reg():
    while True:
        un = input('请输入您的用户名:【注册】')
        for item in user_list:
            if un == item['user']:
                print('用户已存在,请检查')
                break  # break 出for循环
        else:
            pw = input('请输入您的密码:【注册】')
            if len(pw) < 6:
                print('密码长度小于6位,请检查')
            else:
                # 用户注册成功
                user_list.append({'user': un, 'password': pw, 'balance': 3000})
                print('恭喜您注册成功!')
                return True  # 退出整个函数

2.登录

代码如下:

def login():
    while True:
        un = input('请输入您的用户名:【登陆】')
        pw = input('请输入您的密码:【登陆】')
        for user in user_list:
            if user['user'] == un and user['password'] == pw: # 这就说明用户名和密码输入正确
                print('恭喜您登陆成功!')
                global current_user
                current_user = user
                return
        else:
            print('用户名或密码错误!')

3.查询余额

代码如下:

def check_balance():
    if current_user:  # 代表当前已经登陆
        print('当前用户的余额为:', current_user['balance'])
    else:
        print('请先登陆后再进行查询余额操作!')

4.存款

代码如下:

def depoisit():
    if current_user:
        money = int(input('请输入您要存款的金额:'))
        if money[-2:] == '00' and len(money) > 2:  # 说明输入的是100的整数倍
            current_user['balance'] += int(money)
            print('恭喜存款成功,当前余额为:', current_user['balance'])
        else:
            print('您的存款金额格式不正确,请检查后再操作')
    else:
        print('您尚未登陆,请登陆后再进行相关操作!')

5.取款

代码如下:

def withdraw():
    if current_user:
        money = input('请输入您要取款的金额:')
        if money[-2:] == '00' and len(money) > 2:  # 说明输入的是100的整数倍
            if current_user['balance'] >= int(money):
                current_user['balance'] -= int(money)
                print('恭喜您取款成功,当前余额为:', current_user['balance'])
            else:
                print('您的余额不足!')
        else:
            print('您的存款金额格式不正确,请检查后再操作')
    else:
        print('您尚未登陆,请登陆后再进行相关操作!')

6.转账

代码如下:

def transfer():
    if current_user:
        to_user = input('请输入对方的账号:')
        if current_user['user'] == to_user:
            print('不能自己给自己转账,请检查后再操作')
        else:
            for user in user_list:
                if user['user'] == to_user:  # 说明找到了一个有效的转账用户
                    money = input('请输入您要转账的金额:')
                    if money[-2:] == '00' and len(money) > 2:  # 说明输入的是100的整数倍
                        if current_user['balance'] >= int(money):
                            current_user['balance'] -= int(money)  # 自己账户余额减少
                            user['balance'] += int(money)          # 对方账户余额增加
                            print('恭喜您转账成功,当前余额为:', current_user['balance'])
                        else:
                            print('您的余额不足,无法转账,赶紧去搬砖吧!')
                    else:
                        print('您的存款金额格式不正确,请检查后再操作')
                    break
            else:
                print('对方账户不存在,请检查后再操作!')
    else:
        print('您尚未登陆,请登陆后再进行相关操作!')

二、完整代码

user_list = [
    {'user': 'zhangsan', 'password': '123456', 'balance': 1000},
    {'user': 'lisi', 'password': '111111', 'balance': 2500},
    {'user': 'wangwu', 'password': '252525', 'balance': 100}
]

current_user = None  # 用于记录当前登陆用户信息的全局变量

def reg():
    while True:
        un = input('请输入您的用户名:【注册】')
        for item in user_list:
            if un == item['user']:
                print('用户已存在,请检查')
                break  # break 出for循环
        else:
            pw = input('请输入您的密码:【注册】')
            if len(pw) < 6:
                print('密码长度小于6位,请检查')
            else:
                # 用户注册成功
                user_list.append({'user': un, 'password': pw, 'balance': 3000})
                print('恭喜您注册成功!')
                return True  # 退出整个函数

def login():
    while True:
        un = input('请输入您的用户名:【登陆】')
        pw = input('请输入您的密码:【登陆】')
        for user in user_list:
            if user['user'] == un and user['password'] == pw: # 这就说明用户名和密码输入正确
                print('恭喜您登陆成功!')
                global current_user
                current_user = user
                return
        else:
            print('用户名或密码错误!')


def check_balance():
    if current_user:  # 代表当前已经登陆
        print('当前用户的余额为:', current_user['balance'])
    else:
        print('请先登陆后再进行查询余额操作!')


def depoisit():
    if current_user:
        money = int(input('请输入您要存款的金额:'))
        if money[-2:] == '00' and len(money) > 2:  # 说明输入的是100的整数倍
            current_user['balance'] += int(money)
            print('恭喜存款成功,当前余额为:', current_user['balance'])
        else:
            print('您的存款金额格式不正确,请检查后再操作')
    else:
        print('您尚未登陆,请登陆后再进行相关操作!')


def withdraw():
    if current_user:
        money = input('请输入您要取款的金额:')
        if money[-2:] == '00' and len(money) > 2:  # 说明输入的是100的整数倍
            if current_user['balance'] >= int(money):
                current_user['balance'] -= int(money)
                print('恭喜您取款成功,当前余额为:', current_user['balance'])
            else:
                print('您的余额不足!')
        else:
            print('您的存款金额格式不正确,请检查后再操作')
    else:
        print('您尚未登陆,请登陆后再进行相关操作!')


def transfer():
    if current_user:
        to_user = input('请输入对方的账号:')
        if current_user['user'] == to_user:
            print('不能自己给自己转账,请检查后再操作')
        else:
            for user in user_list:
                if user['user'] == to_user:  # 说明找到了一个有效的转账用户
                    money = input('请输入您要转账的金额:')
                    if money[-2:] == '00' and len(money) > 2:  # 说明输入的是100的整数倍
                        if current_user['balance'] >= int(money):
                            current_user['balance'] -= int(money)  # 自己账户余额减少
                            user['balance'] += int(money)          # 对方账户余额增加
                            print('恭喜您转账成功,当前余额为:', current_user['balance'])
                        else:
                            print('您的余额不足,无法转账,赶紧去搬砖吧!')
                    else:
                        print('您的存款金额格式不正确,请检查后再操作')
                    break
            else:
                print('对方账户不存在,请检查后再操作!')
    else:
        print('您尚未登陆,请登陆后再进行相关操作!')


def get_menu():
    menu = '''
          ******欢迎来到WoniuATM*******

         *********请选择操作菜单*********

         *****1. 注册 2. 登录 3. 查询余额 4. 存款 5. 取款  6.转账  7.取卡  ***
    '''
    while True:
        print(menu)
        option = input('请输入您要操作的菜单:')
        if option == '1':
            reg()
        elif option == '2':
            login()
        elif option == '3':
            check_balance()
        elif option == '4':
            depoisit()
        elif option == '5':
            withdraw()
        elif option == '6':
            transfer()
        elif option == '7':
            print('感谢您的使用,欢迎下次再来!')
            break
        else:
            print('选择菜单项错误,请重新选择!')

get_menu()

总结


例如:以上就是今天要分享的内容,本文仅仅简单介绍了如何去写一个面向对象编程,里面用到了很多python的基本语法,同时要注意代码格式缩进和一些代码逻辑的实现。有问题的话欢迎留言。

猜你喜欢

转载自blog.csdn.net/wuyomhchang/article/details/123743108