How to fix Attribute error generated from child class in python

Viewed 75

I am creating a simple bank system; in my code, I want the menu function in the parent class to run first, then when the user picks an option, it goes to the child class Accounts. I created a new property in the child class self.bankdata The methods I created, make use of the property. But when I run it and it gets to the point it's meant to print the bank data. It displays an error AttributeError: 'Customer' object has no attribute 'bankdata'. I am confused because it's meant to work since I added the instance variable to the init.Please, how can I fix this, as I'm a beginner with 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()

Expected Result is meant to print the self.bankdata dictionary created. Also, I am still going to have another class where I would need to inherit the bankdata from the Accounts class.

1 Answers

It's right, there is no attribute bankdata in Customer, you must create an Accounts instance instead :

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