patternpythonMinor
Bank ATM program in Python
Viewed 0 times
bankatmprogrampython
Problem
For homework, I have to code a program in Python that effectively simulates a bank ATM.
```
print('Welcome to Northen Frock Bank ATM')
restart=('Y')
chances = 3
balance = 67.14
while chances >= 0:
pin = int(input('Please Enter You 4 Digit Pin: '))
if pin == (1234):
print('You entered you pin Correctly\n')
while restart not in ('n','NO','no','N'):
print('Please Press 1 For Your Balance\n')
print('Please Press 2 To Make a Withdrawl\n')
print('Please Press 3 To Pay in\n')
print('Please Press 4 To Return Card\n')
option = int(input('What Would you like to choose?'))
if option == 1:
print('Your Balance is £',balance,'\n')
restart = input('Would You you like to go back? ')
if restart in ('n','NO','no','N'):
print('Thank You')
break
elif option == 2:
option2 = ('y')
withdrawl = float(input('How Much Would you like to
withdraw? \n£10/£20/£40/£60/£80/£100 for other enter 1: '))
if withdrawl in [10, 20, 40, 60, 80, 100]:
balance = balance - withdrawl
print ('\nYour Balance is now £',balance)
restart = input('Would You you like to go back? ')
if restart in ('n','NO','no','N'):
print('Thank You')
break
elif withdrawl != [10, 20, 40, 60, 80, 100]:
print('Invalid Amount, Please Re-try\n')
restart = ('y')
elif withdrawl == 1:
withdrawl = float(input('Please Enter Desired amount:'))
elif option == 3:
Pay_in = float(input('How Much Would You Like To Pay In? '))
balance = balance + Pay_in
print ('\nYour Balance is now £',balance)
res
```
print('Welcome to Northen Frock Bank ATM')
restart=('Y')
chances = 3
balance = 67.14
while chances >= 0:
pin = int(input('Please Enter You 4 Digit Pin: '))
if pin == (1234):
print('You entered you pin Correctly\n')
while restart not in ('n','NO','no','N'):
print('Please Press 1 For Your Balance\n')
print('Please Press 2 To Make a Withdrawl\n')
print('Please Press 3 To Pay in\n')
print('Please Press 4 To Return Card\n')
option = int(input('What Would you like to choose?'))
if option == 1:
print('Your Balance is £',balance,'\n')
restart = input('Would You you like to go back? ')
if restart in ('n','NO','no','N'):
print('Thank You')
break
elif option == 2:
option2 = ('y')
withdrawl = float(input('How Much Would you like to
withdraw? \n£10/£20/£40/£60/£80/£100 for other enter 1: '))
if withdrawl in [10, 20, 40, 60, 80, 100]:
balance = balance - withdrawl
print ('\nYour Balance is now £',balance)
restart = input('Would You you like to go back? ')
if restart in ('n','NO','no','N'):
print('Thank You')
break
elif withdrawl != [10, 20, 40, 60, 80, 100]:
print('Invalid Amount, Please Re-try\n')
restart = ('y')
elif withdrawl == 1:
withdrawl = float(input('Please Enter Desired amount:'))
elif option == 3:
Pay_in = float(input('How Much Would You Like To Pay In? '))
balance = balance + Pay_in
print ('\nYour Balance is now £',balance)
res
Solution
The best way to do this would be to take each of your sections, and give them each a function of their own. Also, when it's possible, separate user input functions from purely logic to improve readability!
For example, it could start looking like this:
Whenever you can avoid nesting, you should. This makes it easier to read.
In this case you can clearly see logging in will open the main_menu (which you will have to write), and entering a correct pin is the only way to log in.
For example, it could start looking like this:
def verify_pin(pin):
if pin == '1234':
return True
else:
return False
def log_in():
tries = 0
while tries < 4:
pin = input('Please Enter Your 4 Digit Pin: ')
if verify_pin(pin):
print("Pin accepted!")
return True
else:
print("Invalid pin")
tries += 1
print("To many incorrect tries. Could not log in")
return False
def start_menu():
print("Welcome to the atm!")
if log_in():
# you will need to make this one yourself!
main_menu()
print("Exiting Program")
start_menu()Whenever you can avoid nesting, you should. This makes it easier to read.
In this case you can clearly see logging in will open the main_menu (which you will have to write), and entering a correct pin is the only way to log in.
Code Snippets
def verify_pin(pin):
if pin == '1234':
return True
else:
return False
def log_in():
tries = 0
while tries < 4:
pin = input('Please Enter Your 4 Digit Pin: ')
if verify_pin(pin):
print("Pin accepted!")
return True
else:
print("Invalid pin")
tries += 1
print("To many incorrect tries. Could not log in")
return False
def start_menu():
print("Welcome to the atm!")
if log_in():
# you will need to make this one yourself!
main_menu()
print("Exiting Program")
start_menu()Context
StackExchange Code Review Q#46482, answer score: 9
Revisions (0)
No revisions yet.