Hey,
I have been learning python in codecademy for some weeks now (i haven’t finished the course) but I wanted to put what i’ve already learned into action. I am trying to make a kind of ATM machine but the function(Highlighted below) for the services isn’t working properly (it always jumps to else) and i’m having a hard time figuring it out. If you guys help me I would appreciate it.
THIS IS THE CODE:
#all-variables
passcode = 1111
attempts = 0
balance = 100
#Clean screen function
def cls():
print ('\n' * 40)
#Main menu function
def mainMenu():
print "------------------------------------------------"
print "Select one of this options"
print "1. Check my balance"
print "2. Withdraw Money"
print "3. Deposit Money "
print "0. Exit "
print "------------------------------------------------"
options()
**#services function **
** def options():**
** selection=raw_input("->: ")**
** #option 1**
** if selection == 1:**
** cls()**
** print “You currently have " + str(balance) + " in your account”**
** print**
** response= raw_input(“Would you like to return to the main menu ?”).lower()**
** if response.lower() == “Yes”:**
** mainMenu()**
** else:**
** cls()**
** print “Thank you ! Please remember to remove your card”**
** exit()**
** #Option 2 **
** elif selection == 2:**
** cls()**
** amount= raw_input("How much would you like to withdraw?: ")**
** if amount <= balance:**
** balance -= amount**
** print**
** print “Please take your money”**
** elif amount > balance:**
** print**
** print “Sorry ! The amount you wish to withdraw is greater than your balance”**
** else:**
** print**
** print “I didn’t understand your command”**
** mainMenu()**
** #option 3 **
** elif selection == 3:**
** cls()**
** deposAmount = raw_input("How much do you have to deposit? ")**
** if deposAmount > 0:**
** balance += deposAmount**
** print “You deposit was sucessful”**
** else:**
** cls()**
** “Error! Please only insert notes and coins”**
** mainMenu()**
** #option 4 **
** elif selection == 0:**
** cls()**
** print “Thank You ! Please remove your card”**
** exit()**
** else:**
** cls()**
** print “------------------------------------------------”**
** print “Error! Command not understood. Please try again”**
** print “------------------------------------------------”**
** mainMenu()**
#Welcome screen
print "Hello ! Welcome to JD's bank"
print
print "Insert bank card and press any key to procede"
print
raw_input()
#passcode insertion process
while passcode == 1111:
passcodeInsertion= raw_input("Please insert your 4-digit code: ")
print""
if passcodeInsertion == str(passcode):
cls()
passcode = 1234
print "----------> Passcode Correct <-------"
print ""
mainMenu()
elif attempts < 2:
cls()
print "Acess Denied ! Wrong Passcode"
attempts += 1
print "------------------------------------------------"
print ""
print"Try again !! This is your " + str(attempts) + " attempt"
print
print "------------------------------------------------"
print
else:
print""
print "Your card is unfortunately now blocked"
exit()
Hi, @cassiodias .
It is important to format code in its entirely when you post it. That enables users to see your indentation and other important details. You got part of it formatted, but much of it is not.
See How do I format code in my posts?.
The raw_input
function returns a str
ing, as is the case here …
selection=raw_input("->: ")
However, you are testing for an int
here, and in code that follows …
if selection == 1:
Either use the int
function to convert the input to an int
prior to testing, or test for str
ings instead.
2 Likes
Alright ! I’ll take not of this for next time. And thank you very much for your help.
I tried converting the strings to integer and still it doesn’t work.
t#services function
def options():
selection = int(input("Enter a number: "))
#option 1
if selection == 1:
cls()
print "You currently have " + str(balance) + " in your account"
print
response= raw_input("Would you like to return to the main menu ?").lower()
if response.lower() == "Yes":
mainMenu()
else:
cls()
print "Thank you ! Please remember to remove your card"
exit()
#Option 2
elif selection == 2:
cls()
amount= raw_input("How much would you like to withdraw?: ")
if amount <= balance:
balance -= amount
print
print "Please take your money"
elif amount > balance:
print
print "Sorry ! The amount you wish to withdraw is greater than your balance"
else:
print
print "I didn't understand your command"
mainMenu()
#option 3
elif selection == 3:
cls()
deposAmount = raw_input("How much do you have to deposit? ")
if deposAmount > 0:
balance += deposAmount
print "You deposit was sucessful"
else:
cls()
"Error! Please only insert notes and coins"
mainMenu()
#option 4
elif selection == 0:
cls()
print "Thank You ! Please remove your card"
exit()
else:
cls()
print "------------------------------------------------"
print "Error! Command not understood. Please try again"
print "------------------------------------------------"
mainMenu()
This condition will never be True
…
if response.lower() == "Yes":
You are assigning a str
to amount
, here …
amount= raw_input("How much would you like to withdraw?: ")
… and to deposAmount
here …
deposAmount = raw_input("How much do you have to deposit? ")
Check over all your code for errors.
I’ve checked for errors and everything seems to be working, however, the issue i’m facing now is that the Balance = 100 Variable is not updating when I withdraw nor when I deposit money, even when I placed it inside the service function or withdraw function.
#all-variables
passcode = 1111
attempts = 0
balance = 100
#Clean screen function
def cls():
print ('\n' * 40)
#Main menu function
def mainMenu():
print "------------------------------"
print "Select one of this options"
print "1. Check my balance"
print "2. Withdraw Money"
print "3. Deposit Money "
print "0. Exit "
print "------------------------------"
options()
def checkingBalance():
cls()
print "You currently have £" + str(balance) + " in your account"
print
returning()
def withdrawal():
cls()
amount= int(raw_input("How much would you like to withdraw?: £"))
if balance >= amount:
if balance > 0:
balance -= amount
print
print "----------------------"
print "Please take your money"
print "----------------------"
print
returning()
else:
cls()
print "------------------------------------------------------------------"
print "Error ! Please only enter positive numbers. Returning to main menu"
print "------------------------------------------------------------------"
print
mainMenu()
elif balance < amount :
print
print "--------------------------------------------------------------------"
print "Error ! The amount you wish to withdraw is greater than your balance"
print "--------------------------------------------------------------------"
print
returning()
else:
print
print
print "I didn't understand your command"
mainMenu()
#services function
def options():
selection= int(raw_input("->: "))
#option 1
if selection == 1:
checkingBalance()
#Option 2
elif selection == 2:
withdrawal()
#option 3
elif selection == 3:
cls()
deposAmount = int(raw_input("How much do you have to deposit? £"))
if deposAmount > 0:
balance += deposAmount
print
print "-------------------------"
print "You deposit was sucessful"
print "-------------------------"
print
returning()
else:
cls()
print "Error! Please only insert notes and coins"
mainMenu()
#option 4
elif selection == 0:
cls()
print
print "Thank You ! Please remove your card"
print
exit()
else:
cls()
print "------------------------------------------------"
print "Error! Command not understood. Please try again"
print "------------------------------------------------"
mainMenu()
#Function to return to MM or Removing card
def returning():
response= raw_input("Would you like to return to the main menu ?").lower()
if response == "yes":
cls()
mainMenu()
elif response =="no":
cls()
print "Thank you ! Please remember to remove your card"
exit()
else:
cls()
print "-----> Error ! Command not understood. Please try again <-----"
print
returning()
#Welcome screen
print "Hello ! Welcome to JD's bank"
print
print "Insert bank card and press any key to procede"
print
raw_input()
#passcode insertion process
while passcode == 1111:
passcodeInsertion= raw_input("Please insert your 4-digit code: ")
print""
if passcodeInsertion == str(passcode):
cls()
passcode = False
print "--------> Passcode Correct <-------"
print ""
mainMenu()
elif attempts < 2:
cls()
print "Access Denied ! Wrong Passcode"
attempts += 1
print "------------------------------------------------"
print ""
print"Try again !! This is your " + str(attempts) + " attempt"
print
print "------------------------------------------------"
print
else:
print""
print "Your card is unfortunately now blocked"
exit()
Hello @cassiodias ,
On the fourth line of your program, you define balance
in a global scope, as follows …
balance = 100
You can access the value of that global variable within functions, such as options
and withdrawal
, but when you try to assign a new value to balance
within a function, the Python interpreter considers that assignment to be an attempt to access a local variable also named balance
. Since such a local variable does not exist within those functions, you get an error if it is an augmented assignment, such as +=
, because that requires the variable to have a previous value.
You have some choices. One of them is to declare balance
as global within all the functions that change its value. In your program, those functions are options
and withdrawal
. To declare balance
as global, add this line to those two functions, preferably right after the function headers …
global balance
Another choice, and a better one, is to define a BankAccount
class to represent the bank account. Use methods to implement each process such as a deposit, a withdrawal, and a balance inquiry. You might be interested in knowing that Codecademy Pro does actually offer a Python project named Bank Account that uses a BankAccount
class to represent a bank account.
1 Like
Hello,
Thank you for your support you are an tremendous tutor. I did what you recommended and my code is now fully functional and I learned many things from this. Thank You
I will continue my codecademy course and thank you. The world should have more people like you
1 Like