'local variable referenced before assignment in python OOP program
I understand what this error means, but I don't understand why I am getting it. It happens near the bottom of the code, line 76 for example but each time I reference the variable it happens. I called the instances of the class bank account and then set the one the user chooses equal to account, but when I try to use dot notation to access the method on account (for example print("Current balance is: ", account.deposit(pin,amount))) it says the local variable account is referenced before assignment
class BankAccount: #creates a bank account class
def __init__(self, pin): #initializes a constructor method with two parameters, self and pin
self._balance = 0 #initializes balance at 0
self._pin = pin #sets pin as the pin parameter
def deposit(self, pin, amount): #creates deposit method with three parameters. Self, pin and amount
if pin != self._pin: #if pin parameter doesnt match initialized pin
print("Wrong pin")
if amount > 3000: #if the amount is more than 3000
check = int(input("Please enter pin again to deposit large amount: ")) #requests pin again for large amount
if check != self._pin: #if the user enters the wrong pin the second time
print("Pin is incorrect. Transaction cancelled.")
elif check == self._pin: #if user enters correct pin
self._balance += amount #add amount to current balance
return self._balance #return balance
elif amount <= 3000 and amount>0: #if the amount is less or equal to 30000
self._balance += amount #add amount to current balance
return self._balance #return balance
else:
print("invalid input") #if user enters anything else its invalid
def withdraw(self, pin, amount): #creates withdraw method with three parameters; self, pin and amount
if pin != self._pin: #if user enters wrong pin
print("Wrong pin. Try Again")
return
if amount > self._balance: #if trying to withdraw more than in account
print("You are attemtping to withdraw more than is in your account")
self._balance -= amount #takes away amount from balance
return self._balance #returns balance
def get_balance(self,pin): #creates get balance method with two parameters; self and pin
if pin != self._pin: #if user enters wrong pin
print("Wrong pin. Try Again")
return self._balance #returns current balance
def change_pin(self,old_pin,new_pin): #creates change pin method
if old_pin != self._pin: #if user enters wrong old ppin when prompted
print("Wrong Pin")
self._pin = new_pin #set pin equal to enw pin user enters
def main(): #main method to call instances
choice = input("Would you like to open account one or two (press 1 or 2): ")
if choice == 1: #if user chooses account 1
account = BankAccount(8493) #call one instance of bank account class and set equal to account variable
elif choice == 2:#if user chooses account 2
account = BankAccount(8820)#call another instance of bank account class and set equal to account variable
print("Choose one of the options below: ")
print("Press 1 to deposit money")
print("Press 2 to withdraw money")
print("Press 3 to check balance")
print("Press 4 to change pin")
action = int(input("Enter your choice: "))
if action == 1: #if user chooses 1
pin = int(input("Enter your pin: ")) #get value for pin parameter
amount = float(input("Enter amount: ")) #get amount for amount paraneter
print("Current balance is: ", account.deposit(pin,amount)) #accesses the deposit method using dot notation
return
elif action == 2: #if user chooses 2
pin = int(input("Enter your pin: ")) #get value for pin parameter
amount = float(input("Enter amount: "))#get amount for amount paraneter
print("Current balance is: ", account.withdraw(pin,amount))#accesses the withdraw method using dot notation
return
elif action == 3: #if user chooses 3
pin = int(input("Enter your pin: ")) #get value for pin parameter
print("Balance is: ", account.get_balance(pin))#accesses the get balance method using dot notation
elif action == 4: #if user chooses 4
old_pin = int(input("Enter your old pin: ")) #prompts user to enter pin
new_pin = int(input("Enter new pin: ")) #prompts user to enter new pin
account.change_pin(pin,new_pin) #accesses the get change pin method using dot notation
main() #calls main function
Solution 1:[1]
The value received from in-built input function is string and not integer. You need to compare string in the if and elif conditions and not integer so that the account variable is initialized.
choice = input("Would you like to open account one or two (press 1 or 2): ")
if choice == '1': #if user chooses account 1
account = BankAccount(8493) #call one instance of bank account class and set equal to account variable
elif choice == '2':#if user chooses account 2
account = BankAccount(8820)#call another instance of bank account class and set equal to account variable
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sdq |
