0

I am new to python and trying to create a small atm like project using classes. I wanna have the user ability to input the amount they want to withdraw and if the amount exceeds the current balance, it prints out that you cannot have negative balance and asks for the input again.

class account:
    
    def __init__(self,owner,balance):
        self.owner = owner
        self.balance = balance
        # self.withdraw_amount = withdraw_amount
        # self.deposit_amount = deposit_amount
        print("{} has {} in his account".format(owner,balance))

    def withdraw(self,withdraw_amount):
        withdraw_amount = int(input("Enter amount to withdraw: "))
        self.withdraw_amount = withdraw_amount
        if self.withdraw_amount > self.balance:
            print('Balance cannot be negative')
        else:
            self.final_amount = self.balance - self.withdraw_amount
            print("Final Amount after Withdraw " + str(self.final_amount))

    def deposit(self,deposit_amount):
        deposit_amount = int(input("Enter amount to Deposit: "))
        self.deposit_amount = deposit_amount
        self.final_balance = self.balance + self.deposit_amount
        print("Final Amount after Deposit " + str(self.final_balance))

my_account = account('Samuel',500) 
my_account.withdraw(withdraw_amount)

But i am getting the followng error : NameError: name 'withdraw_amount' is not defined

Can someone please point out what i am doing wrong and how can i fix it? Thank you.

2
  • withdraw_amount is not defined before my_account.withdraw(withdraw_amount), Try my_account.withdraw(int(input("Enter amount to withdraw: "))) and remove the input() call from the method. Commented May 9, 2021 at 7:11
  • in your question about upgrading mysql 5.7 in ubuntu 16.04, see dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/… - download the gpg key with apt-key and add a mysql.list containing deb http://repo.mysql.com/apt/ubuntu/ xenial mysql-5.7 ubuntu no longer supports 16.04 as of last month, so don't expect the ubuntu repositories to have any more updates Commented May 24, 2021 at 16:22

2 Answers 2

0

As you said, you want to have input inside the function, therefore you don't have to send any input with the function that you are calling.

2nd thing is that you want to force the user to input again if they enter value and this happens

amount exceeds the current balance, it prints out that you cannot have negative balance and asks for the input again

then you can use a while loop with break condition.

This would work:

class account:
    
    def __init__(self,owner,balance):
        self.owner = owner
        self.balance = balance
        # self.withdraw_amount = withdraw_amount
        # self.deposit_amount = deposit_amount
        print("{} has {} in his account".format(owner,balance))

    def withdraw(self):
        while True:
            try:
                withdraw_amount = int(input("Enter amount to withdraw: "))
                self.withdraw_amount = withdraw_amount
                if self.withdraw_amount > self.balance:
                    print('Balance cannot be negative')
                else:
                    self.final_amount = self.balance - self.withdraw_amount
                    print("Final Amount after Withdraw " + str(self.final_amount))
                    break
            except:
                print('Incorrect input')

    def deposit(self,deposit_amount):
        deposit_amount = int(input("Enter amount to Deposit: "))
        self.deposit_amount = deposit_amount
        self.final_balance = self.balance + self.deposit_amount
        print("Final Amount after Deposit " + str(self.final_balance))

my_account = account('Samuel',500)
my_account.withdraw()
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the response. However, if i input an invalid input like a string, it just exits out. So for example for the input i typed in w and got ValueError: invalid literal for int() with base 10: 'w'
@TreeHuggerRick You didn't mention this thing in question. But check the edit now.
Thanks. I was just looking into the try and except block. Really appreciate the help, Accepting this as an answer.
@TreeHuggerRick Glad to help you.
0

You have to give some value instead of passing the variable withdraw_amount

For example: Your withdraw function have two argument self and withdraw_amount, and when you call it in the line my_account.withdraw(withdraw_amount) you are doing it right but instead of passing variable withdraw_amount just pass some value like 200,300

class account:

def __init__(self,owner,balance):
    self.owner = owner
    self.balance = balance
    # self.withdraw_amount = withdraw_amount
    # self.deposit_amount = deposit_amount
    print("{} has {} in his account".format(owner,balance))

def withdraw(self,withdraw_amount):
    withdraw_amount = int(input("Enter amount to withdraw: "))
    self.withdraw_amount = withdraw_amount
    if self.withdraw_amount > self.balance:
        print('Balance cannot be negative')
    else:
        self.final_amount = self.balance - self.withdraw_amount
        print("Final Amount after Withdraw " + str(self.final_amount))

def deposit(self,deposit_amount):
    deposit_amount = int(input("Enter amount to Deposit: "))
    self.deposit_amount = deposit_amount
    self.final_balance = self.balance + self.deposit_amount
    print("Final Amount after Deposit " + str(self.final_balance))

my_account = account('Samuel',500) 
withdraw_amount = 100
my_account.withdraw(withdraw_amount)

Reason for the error:

withdraw_amount is not defined before my_account.withdraw(withdraw_amount), Try my_account.withdraw(int(input("Enter amount to withdraw: "))) and remove the input() call from the method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.