首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AttributeError:“Account”对象没有属性“account_number”

AttributeError:“Account”对象没有属性“account_number”
EN

Stack Overflow用户
提问于 2020-03-28 13:30:16
回答 1查看 2.8K关注 0票数 0

我想要创建一个小型银行系统,在其中用户进入银行,然后将提供选项,以打开一个新的帐户或使用一个现有的帐户。因此,将调用函数。

我正在使用PyCharm IDE,确保空格是正确的。

我的错误消息,显示我无法访问account_number

AttributeError:“Account”对象没有属性“account_number”

代码语言:javascript
复制
import random as r
class Account():
    print("Welcome to AV Bank")
    def __init__(self,choice):
        self.choice = choice
    def open_account(self):
        self.new_account_holder_name = str(input("Enter new account holder name: "))
        self.account_number = r.randrange(999999)
        print(f"Mr.{self.new_account_holder_name} your account has been successfully opened and account number is {self.account_number}, With balance 0. Please deposit minimum R.1000/-")
    def existing_holder(self):
        self.account_holder_name = str(input("Enter account holder name: "))
        print(f"Welcome Mr.{self.account_holder_name}. Following are the choices. Please select : \nPress 1 deposit money \nPress 2 for withdraw money")
    def deposit(self,amount_added):
        self.balance = 0
        self.amount_added = amount_added
        self.balance = self.balance + self.amount_added
        print(f"Added Rs. {self.amount_added} in account number {self.account_number}")
    def withdraw(self,subtract):
        self.balance = 0
        self.subtract = subtract
        if (self.balance<self.subtract):
            print("In sufficient balance in account")
        else:
            self.balance = self.balance - self.subtract
            print("Withrawal accepted")
            return self.balance
    def __str__(self):
        return (f"Account Owner: {self.owner} \nAccount Balance: {self.balance}")

customer_visit = int(input("Press 1: To Open New Bank Account or Press 2: To Use Existing Account : "))
acct1 = Account(customer_visit)
if customer_visit ==1:
    acct1.open_account()
    amount_added = int(input("Enter the deposit amount: "))
    acct1.deposit(amount_added)
if customer_visit ==2:
    acct1.existing_holder()
    holder_option = int(input())
    if holder_option ==1:
        amount_added = int(input("Enter the deposit amount: "))
        acct1.deposit(amount_added)
    if holder_option ==2:
        withdraw_money = int(input("Enter amount you want to withdraw: "))
        acct1.withdraw(withdraw_money)
EN

回答 1

Stack Overflow用户

发布于 2020-03-28 13:39:34

这是因为没有将account_number设置为类属性。因此,在调用open_account()方法之后,就会立即忘记它。如果希望稍后调用account_number,则需要将其设置为属性。

代码语言:javascript
复制
def open_account(self):
    self.new_account_holder_name = str(input("Enter new account holder name: "))
    self.account_number = r.randrange(999999)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60901782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档