如何修复 python 中子 class 生成的属性错误
How to fix Attribute error generated from child class in python
我正在创建一个简单的银行系统;在我的代码中,我希望父 class 中的菜单功能首先转到 运行,然后当用户选择一个选项时,它转到子 class 帐户。我在子 class 中创建了一个新的 属性 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()
Expected Result 用于打印创建的 self.bankdata 字典。此外,我仍将有另一个 class,我需要从帐户 class.
继承银行数据
对了,Customer
里面没有属性bankdata
,
您必须创建一个 Accounts
实例来代替:
C1 = Accounts(...,...,...)
我正在创建一个简单的银行系统;在我的代码中,我希望父 class 中的菜单功能首先转到 运行,然后当用户选择一个选项时,它转到子 class 帐户。我在子 class 中创建了一个新的 属性 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()
Expected Result 用于打印创建的 self.bankdata 字典。此外,我仍将有另一个 class,我需要从帐户 class.
继承银行数据对了,Customer
里面没有属性bankdata
,
您必须创建一个 Accounts
实例来代替:
C1 = Accounts(...,...,...)