Python函数实现银行自助存取款系统

代码:

#创建用户账号管理库
Account = [{'Name':'Jack','Password':'123456','Balance':3000}]
#定义主界面函数
def home_page():
    print('欢迎使用银行ATM自助存取款系统')
    print('''
              1.登陆账号
              2.注册账号
              3.退出系统
      ''')
#定义登录功能界面函数
def option_page():
    print('您好%s' % Account[i]['Name']+',欢迎使用银行ATM自助存取款系统')
    print('''
             1.查询余额     2.自助取款
             3.自助存款     4.修改密码
             5.回主菜单     6.退出系统
          ''')
#定义主界面判断函数
def judge1():
    while 1:
        home_page()
        judge_1 = input('请选择您需要的服务:')
        if judge_1 == '1':
            user_login()     #执行用户的登陆
        elif judge_1 == '2':
            name()
            new_account()
            judge1()
        elif judge_1 == '3':
            print('感谢您的使用,欢迎再次光临!')
            input('>>按确定键退出服务<<')
            exit()
        else:
            print('请输入选项中的有效数字!')
            input('>>按确定键继续为您服务<<')

#定义一个功能界面判断函数
def judge2():
    while 1:
        option_page()  # 进入功能界面
        judge_2 = input('请选择您需要的服务:')
        if judge_2 == '1':
            print('您的账号余额为:%0.2f' % Account[i]['Balance'])
            input('>>按确定键继续为您服务<<')
        elif judge_2 == '2':
            sum = int(input('请输入你的取款金额:'))
            if Account[i]['Balance']-sum >= 0:  #判断余额是否足够
                balance = Account[i]['Balance'] - sum
                dict1 = {'Balance':balance}
                Account[i].update(dict1)  #更新账户余额
                print('恭喜您取款成功,取款金额为:%d,账户余额为:%d' % (sum,balance))
                input('>>按确定键继续为您服务<<')
            else:
                print('您的账号余额不足!')
                input('>>按确定键继续为您服务<<')
        elif judge_2 == '3':
            sum = int(input('请输入你的存款金额:'))
            total = sum + Account[i]['Balance']
            dict2 = {'Balance':total}
            Account[i].update(dict2)  #更新存款金额
            print('恭喜您存款成功,您的存款金额为:%d,账户余额为:%d' % (sum,total))
            input('>>按确定键继续为您服务<<')
 
        elif judge_2 == '4':
            rep_password()#进入修改密码函数
            input('>>按确定键继续为您服务<<')
        elif judge_2 == '5':
            judge1()#进入主菜单判断函数
        elif judge_2 == '6':
            print('感谢您的使用,欢迎再次光临!')
            input('>>按确定键退出服务<<')
            exit()
        else:
            print('请输入选项中的有效数字!')
            input('>>按确定键继续为您服务<<')
#定义一个登陆用户名判断函数
def user_login():
    k = 3  #定义允许用户输入错误的次数的变量
    while k:
        global i
        name()  #首先遍历账户信息,然后生成列表用于后面判断
        username = input('请输入您的用户名:')
        password = input('请输入6位用户密码:')
        for i in range(len(Account)):
            if username in list1_username and password in list2_password:  #遍历出来的用户名和密码在列表中,执行
                if username == Account[i]['Name']:
                    if password == Account[i]['Password']:  # 用户名和密码匹配一致,执行
                        judge2()  #进入功能界面判断
                        break
                    else:  #用户名和密码不匹配,执行
                        print('用户名和密码不匹配,请重新输入!')
                    #break
            elif username == '' and password == '':  # 密码和用户输入为空,执行
                print('用户名和密码不能为空,请重新输入!')
                break
            elif username not in list1_username or password not in list2_password:  # 用户名或密码不在列表中,执行
                k -= 1
                if k == 1:
                    print('用户名或密码输入错误已达2次,再次输入错误将返回主菜单!')
                    break
                elif k == 0:
                    print('用户名或密码输入错误已达3次,系统自动返回主菜单!')
                    input('>>按确定键返回主菜单<<')
                    judge1()
                print('用户名或密码输入错误,请重新输入!')
                break
#定义一个修改密码函数
def rep_password():
    #while 1:
    password = input('请输入6位原密码:')
    #if len(password) == 6 and password.isdigit():
        #print("输入错误,请输入6位数字密码!")
        #rep_password()
    if password == Account[i]['Password']:   #密码与原密码一致,执行
        while 1:
            new_password1 = input('请输入6位新密码:')
            new_password2 = input('请再次输入6位新密码:')
            if new_password1 == '' and new_password2 == '':
                print('密码不能为空,请重新输入!')
                continue
            # 如果数字密码是6位一致的密码,执行
            elif new_password1 == new_password2 and (len(new_password2) ==6 and new_password2.isdigit()):
                dict2 ={'Password':new_password2}
                #Account[i]['password'] = new_password2
                Account[i].update(dict2)
                print('恭喜您,密码修改成功!')
                break
            # 如果数字密码不是6位一致的密码,执行
            elif new_password1 == new_password2 and (len(new_password2) !=6 or new_password2.isdigit()):
                print('密码长度输入错误,请输入6位有效数字密码!')
                continue
            # 如果不是数字密码,执行
            elif new_password1 == new_password2 and (len(new_password2) ==6 or new_password2.isdigit()):
                print('密码应为纯数字密码,请输入6位有效数字密码!')
                continue
            # 判断两次密码输入是否一致,不一致重新输入
            elif new_password1 != new_password2:
                print('两次输入的密码不一致,请重新输入!')
                continue
    # 输入密码与原密码不一致,执行
    elif password != Account[i]['Password']:
        print('原密码输入错误,请重新输入!')
        rep_password()    # 递归执行密码输入
    else:
        print('请输入有效数字,谢谢!')
#定义一个用户遍历函数
def name():
    global username
    global password
    global list1_username
    global list2_password
    list1_username = []
    list2_password = []
    for j in range(len(Account)):
        all_username = Account[j]['Name']
        list1_username.append(all_username)    # 将遍历出来的用户存到列表
        all_password = Account[j]['Password']
        list2_password.append(all_password)   # 将遍历出来的密码存到列表
#定义一个实现用户注册的函数
def new_account():
    while 1:
        # 设置新用户名添加到用户库
        while 1:
            new_username = input('请输入您的用户名:')
            if new_username in list1_username:
                print('用户名已存在,请重新输入!')
            elif new_username == '':
                print('用户名不能为空,请重新输入!')
            else:
                new_user = {'Name': new_username}
                Account.append(new_user)
                break
        # 设置新用户名的登陆密码添加到用户库
        while 1:
            new1_password = input('请输入您的6位数字密码:')
            new2_password = input('请再次输入您的6位数字密码:')
            if new1_password == '' and new2_password == '':
                print('密码不能为空,请重新输入!')
            elif new1_password == new2_password:
                if len(new2_password) == 6 and new2_password.isdigit():
                    new_user['Password'] = new2_password
                    print('***恭喜您,新账户注册成功!***')
                    break
                elif len(new2_password) != 6:
                    print('密码长度不正确,请重新输入!')
                elif len(new2_password) == 6 or new2_password.isdigit():
                    print('密码不是纯数字密码,请重新输入')
            elif new1_password != new2_password:
                print('两次输入密码不一致,请重新输入!')
        # 新用户预存金额
        select = input('是否需要预存金额,需要请输入“1”后按确认键:')
        if select == '1':
            new_balance = int(input('请输入您预存金额:'))
            new_user['Balance'] = new_balance
            print('预存款成功!存款金额为:%d' % new_balance)
            input('>>按确定键返回主菜单<<')
            break
        else:
            input('>>按确定键返回主菜单<<')
            break
#主运行函数
while 1:
    judge1()

运行结果:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 11
    点赞
  • 73
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您讲解如何实现一个Python银行存取款系统。以下是一个基本的代码框架: ```python class BankAccount: def __init__(self, name, balance=0): self.name = name self.balance = balance def deposit(self, amount): self.balance += amount print(f"Deposit of {amount} successful. Your new balance is {self.balance}.") def withdraw(self, amount): if amount > self.balance: print("Insufficient funds.") else: self.balance -= amount print(f"Withdrawal of {amount} successful. Your new balance is {self.balance}.") ``` 在这个代码框架中,我们定义了一个 BankAccount 类。在类的初始化函数 `__init__` 中,我们需要传入账和初始余额。如果没有指定初始余额,则默认为0。 我们还定义了两个方法:`deposit` 和 `withdraw`。`deposit` 方法用于向账中存款。它接受一个参数 `amount`,表示要存的金额。存款后,我们在控制台输出存款成功的消息,并显示新的余额。 `withdraw` 方法用于从账中取款。它也接受一个参数 `amount`,表示要取的金额。如果账余额不足,我们在控制台输出余额不足的消息。否则,我们从余额中减去取款金额,并在控制台输出取款成功的消息,并显示新的余额。 我们可以使用以下代码来测试这个类: ```python account = BankAccount("Alice", 1000) account.deposit(500) account.withdraw(2000) ``` 这将创建一个账为 "Alice",初始余额为1000的账。然后,我们向账中存入500元,并从账中取出2000元。在控制台中,我们应该看到以下输出: ``` Deposit of 500 successful. Your new balance is 1500. Insufficient funds. ``` 这表明存款和取款操作都按预期进行了。 当然,这只是一个非常的示例,际上,一个真正的银行系统需要更多功能和安全性措施。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值