【问题标题】:How to fix Attribute error generated from child class in python如何修复python中子类生成的属性错误
【发布时间】:2021-12-23 04:31:57
【问题描述】:

我正在创建一个简单的银行系统;在我的代码中,我希望父类中的菜单功能首先运行,然后当用户选择一个选项时,它会转到子类 Accounts。我在子类self.bankdata 中创建了一个新属性,我创建的方法利用了该属性。但是当我运行它并达到目的时,它意味着打印银行数据。它显示错误AttributeError: 'Customer' object has no attribute 'bankdata'。我很困惑,因为我将实例变量添加到 init 之后它就可以工作了。请问,我该如何解决这个问题,因为我是 Python 的初学者

from random import randrange
from secrets import randbelow
import  collections
import time
# "${:,.2f}".format(float(0))


class Customer:
  def __init__(self, Fname = "Please enter your First name:", Lname = "Please enter your Last name:", email = "Please enter your email:",  balance = 0):
      self.Fname = Fname 
      self.Lname = Lname 
      self.email = email
      self.balance = balance
      
      
  def menu(self):
      print ("-----------------------------------------------")
      print("Welcome to Dangote's Wellings Bank")
      print ("-----------------------------------------------")
      print("1.Create an account \n2.Log in to your acccount \n3.Exit")
      menu_option = int(input())
      if menu_option == 1:
        Accounts.create_account(self)
      if menu_option == 2:
          Accounts.log_in(self)
      if menu_option == 3:
          self.exit_bank()

class Accounts(Customer):
  def __init__(self, Fname, Lname, email, bankdata = collections.defaultdict(dict)):
    super().__init__(Fname, Lname, email)
    self.bankdata = bankdata

  def create_account(self):
    print("\n")
    firstname = input(self.Fname)
    lastname = input(self.Lname)
    global account_name
    account_name = firstname + " " + lastname
    input(self.email)
    
    card_number = f'400000{randrange(1e10):010}'
    pin_request = input(
        "Would you like the bank to issue your unique PIN or create your PIN yourself (Issue/Create): ")

    if pin_request == "Issue":
        pin_number = f'{randbelow(10_000):04}'
    elif pin_request == "Create":
        pin_number = input("Enter your desired 4 DIGIT PIN: ")
    else:
        print("Invalid Input please try again")
        self.create_account()

    print(
        f'\nYour card has been created\nAccount Name:\n{account_name} \nYour card number:\n{card_number}\nYour card PIN:\n{pin_number}')

    #WHERE THE ERROR OCCURS 
    self.bankdata[account_name][card_number] = pin_number
    print(self.bankdata)
    print("\nYou can now log in")
    self.menu()

  def log_in(self):
    for key, value in self.bankdata.items():
      if isinstance(value, dict):
        for card_no, card_pin in value.items():
          if key == account_name:
            global card_login
            card_login = input("Enter your card number: ")
            pin_login = input("Enter your PIN: ")

    if card_login == card_no and pin_login == card_pin:
      print(f'\nYou have successfully logged in')
      Bankingsystem.operation_to_perform(self)
    else:
      print("\n Wrong Card or PIN!")
      time.sleep(2)
      self.menu()

  def log_out(self):
    print("You have successfully logged out")
    self.menu()
  def exit_bank(self):
    print("Thanks for banking with us, we hope to see you next time. Bye!")


C1 = Customer()
C1.menu()

预期结果旨在打印创建的 self.bankdata 字典。此外,我仍然需要从 Accounts 类继承银行数据的另一个类。

【问题讨论】:

  • 您永远不会创建 Accounts 类的实例,因此其 .__init__() 中的任何内容都不会被执行。

标签: python class object inheritance attributeerror


【解决方案1】:

没错,Customer中没有属性bankdata, 您必须创建一个 Accounts 实例:

C1 = Accounts(...,...,...)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 2021-06-05
    • 2018-02-27
    • 2021-12-31
    • 2020-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多