python ATM机 案例代码

2023-01-31 00:01:59 python 代码 案例

利用目前学的流程控制写的


'''
ATM机
需求:
1.登陆
    输入账号输入密码
    每日只有3次登陆密码错误的机会,超过3次禁止登陆
2.查询余额
3.存款
4.取款
5.转帐
6.退出
'''
infos = [
    {'name':'jack','pwd':'123','yue':0},
    {'name':'bran','pwd':'123','yue':0},
    {'name':'eGon','pwd':'123','yue':0},
]

# 禁止登陆
ban = False
# 登陆状态
login_state = False
# 账号密码错误次数
login_num = 0
# 菜单 choice 
choice = 0
# 输入的用户名
name = ''
# 输入的密码
pwd = ''
# 余额
yue = 0

while ban == False:
    # 登陆
    while login_state == False:
        # 登陆
        name = input('请输入账号:')
        pwd = input('请输入密码:')

        # 查询账号密码是否正确
        for info in infos:
            if name == info['name'] and pwd == info['pwd']:
                print('登陆成功!')
                login_state = True
                yue = info['yue']
                break
        # 如果没有账号密码不正确则 运行下面的代码
        if login_state == False:
            login_num += 1
            print('账号密码错误!请重新输入!剩余次数 %s' % (3 - login_num))
            if login_num == 3:
                print('今日登陆错误次数超限,请明日再来')
                ban = True
                break
    # 打印菜单
    if login_state == True and choice == 0:
        print('''
        1 查询
        2 存款
        3 转帐
        4 取款
        5 退出
        ''')
        choice = int(input('请输入操作状态:'))
                
    # 查询
    if login_state == True and choice == 1:
        print('%s 账户的余额为 %s ¥' % (name,yue))
        choice = 0
    
    # 存款
    if login_state == True and choice == 2:
        money = int(input('请输入你的金额:'))
        for info in infos:
            if name == info['name']:
                info['yue'] += money
                yue += money
                print('%s 账户存入 %s ¥' % (name,money))
        choice = 0
    # 转帐:
    if login_state == True and choice == 3:
        to_state = 0 # 1:成功 2:余额不足
        to_num = 0
        while to_state == 0:
            to_name = input('请输入转入的用户名:')
            # 判断 用户是否存在
            for info in infos:
                if to_name == info['name']:
                    money = int(input('请输入转帐的金额:'))
                    # 判断自己账户的金额是否足够
                    if yue >= money:
                        info['yue'] += money
                        yue -= money
                        print('你给 %s 用户成功转入 %s ¥'%(to_name,money))
                        to_state = 1
                        break
                    else:
                        print('你的余额不足,请充值后再转帐')
                        to_state = 2
            if to_state == 1:
                break
            elif to_state == 2:
                break
            else:
                to_num += 1
                print('你输入的用户不存在,请重新输入:')
        choice = 0
        
    # 取款
    if login_state == True and choice == 4:
        money = int(input('请输入你的取款金额:'))
        # 判断输入的金额是否小于等于余额
        if money <= yue:
            print('请再出钞口取钞票吧!')
            yue -= money
            choice = 0
        else:
            print('你输入的金额大于你的余额,无法取款!')

    # 退出
    if login_state == True and choice == 5:
        break

相关文章