簡體   English   中英

如何修復python中子類生成的屬性錯誤

[英]How to fix Attribute error generated from child class in python

我正在創建一個簡單的銀行系統; 在我的代碼中,我希望先運行父類中的菜單功能,然后當用戶選擇一個選項時,它會轉到子類 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 類繼承銀行數據。

沒錯, Customer沒有屬性bankdata ,您必須創建一個Accounts實例:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM