这是我的代码:
class BankAccount:
def __init__(self, FirstName, LastName, AccNum, Balance, CreationYear, CreationMonth, CreationDay):
self.FirstName = FirstName
self.LirstName = LirstName
self.AccNum = AccNum
self.Balance = Balance
self.CreationYear = CreationYear
self.CreationMonth = CreationMonth
self.CreationDay = CreationDay
def AddAccount(self):
self.FirstName = input("First Name: ")
self.LastName = input("Last Name: ")
self.AccNum = input("Account Number: ")
self.Balance = input("Balance: ")
self.CreationYear = input("Creatin Year: ")
self.CreationMonth = input("Creation Month: ")
self.CreationDay = input("Creation Day: ")
return self.FirstName , self.LastName , self.AccNum , self.Balance , self.CreationYear , self.CreationMonth , self. CreationDay
def Deposit(self):
amount = input("How much do you want to Deposit? ")
self.Balance = str(float(amount) + float(self.Balance))
print("Balance: ", self.Balance)
return self.Balance
def Withdrawl(self):
amount = input("How much do you want to withdrawl? ")
if (float(amount) > float(self.Balance)):
print("Insufficent Balance.")
else:
self.Balance = str(float(self.Balance) - float(amount))
print("Balance: ", self.Balance)
return self.Balance
x = BankAccount(FirstName, LastName, AccNum, Balance, CreationYear, CreationMonth, CreationDay)
x.AddAccount()
这是我遇到的错误
Traceback (most recent call last):
File "C:\Users\Dark\Desktop\New folder (5)\bank.py", line 38, in <module>
x = BankAccount(FirstName, LastName, AccNum, Balance, CreationYear, CreationMonth, CreationDay)
NameError: name 'FirstName' is not defined
答案 0 :(得分:0)
类BankAccount
不应用于收集用于创建它的用户数据;您可能应该从类中提取AddAccount
,并使用它作为工厂函数来构建类:
也许是这样的:
class BankAccount:
def __init__(self, FirstName, LastName, AccNum, Balance, CreationYear, CreationMonth, CreationDay):
self.FirstName = FirstName
self.LastName = LastName
self.AccNum = AccNum
self.Balance = Balance
self.CreationYear = CreationYear
self.CreationMonth = CreationMonth
self.CreationDay = CreationDay
def Deposit(self):
amount = input("How much do you want to Deposit? ")
self.Balance = str(float(amount) + float(self.Balance))
print("Balance: ", self.Balance)
return self.Balance
def Withdrawl(self):
amount = input("How much do you want to withdrawl? ")
if (float(amount) > float(self.Balance)):
print("Insufficent Balance.")
else:
self.Balance = str(float(self.Balance) - float(amount))
print("Balance: ", self.Balance)
return self.Balance
def AddAccount():
"""asks input data for the creation of a bank account,
returns the newly created bank account
"""
FirstName = input("First Name: ")
LastName = input("Last Name: ")
AccNum = input("Account Number: ")
Balance = input("Balance: ")
CreationYear = input("Creatin Year: ")
CreationMonth = input("Creation Month: ")
CreationDay = input("Creation Day: ")
return BankAccount(FirstName , LastName , AccNum , Balance , CreationYear , CreationMonth , CreationDay)
x = AddAccount()
或者,您可以将属性初始化为None
或zero
,并让类请求用户输入。但是,使用class BankAccount
处理用户输入可能不是最好的设计...
class BankAccount:
def __init__(self):
self.FirstName = None
self.LastName = None
self.AccNum = None
self.Balance = 0
self.CreationYear = None
self.CreationMonth = None
self.CreationDay = None
def AddAccount(self):
self.FirstName = input("First Name: ")
self.LastName = input("Last Name: ")
self.AccNum = input("Account Number: ")
self.Balance = input("Balance: ")
self.CreationYear = input("Creatin Year: ")
self.CreationMonth = input("Creation Month: ")
self.CreationDay = input("Creation Day: ")
def Deposit(self):
amount = input("How much do you want to Deposit? ")
self.Balance = str(float(amount) + float(self.Balance))
print("Balance: ", self.Balance)
return self.Balance
def Withdrawl(self):
amount = input("How much do you want to withdrawl? ")
if (float(amount) > float(self.Balance)):
print("Insufficent Balance.")
else:
self.Balance = str(float(self.Balance) - float(amount))
print("Balance: ", self.Balance)
return self.Balance
x = BankAccount()
x.AddAccount()
答案 1 :(得分:0)
您忘记定义传递给类构造函数的变量。
您可能要使用3个选项之一:
1)在使用变量之前先定义变量,例如
FirstName = "Jhon"
LastName = "Doe"
... #define rest of variables
x = BankAccount(FirstName, LastName, AccNum, Balance, CreationYear, CreationMonth, CreationDay)
2)用文字来调用类构造函数,例如
x = BankAccount("Jhon", "Doe", "MyAccount", 200, 2019, 6, 2)
3)直接在__init__
内部定义变量,例如
class BankAccount:
def __init__(self):
self.FirstName = input("First Name: ")
self.LastName = input("Last Name: ")
self.AccNum = input("Account Number: ")
self.Balance = input("Balance: ")
self.CreationYear = input("Creatin Year: ")
self.CreationMonth = input("Creation Month: ")
self.CreationDay = input("Creation Day: ")
#rest of code