Reputation:
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.
Upvotes: 0
Views: 53
Reputation: 639
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
Upvotes: 0
Reputation: 1097
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()
Upvotes: 0