python ATM loop trouble - python

I have written this program and cant seem to figure out how to get the program to loop back to the beginning and ask the 'choice' option again.
The program runs fine, everything prints to the screen, even the part that asks if you would like another transaction, how do I get this to loop back?
Write ATM program. Enter account balance, print beginning balance.
ask for deposit or withdrawl, depending on selection, call function
to perform the action they wish, then print new balance. (Use iteration)
def withdraw():
amt = int(input("What amount to withdraw - multiples of $20: "))
print('New balance: $', balance - amt)
def deposit():
amt = int(input("How much are you depositing? "))
print('New balance: $',balance + amt)
def bal():
return(balance)
print ("Hello, Welcome to Python ATM.")
balance = float(65.01)
pin = int(input("please enter your account number (Any number:) "))
print('''Current balance: $''',balance)
choice = int(input('''
Please choose an option from the following:
1 - Withdraw
2 - Deposit
3 - Check Balance
4 - Exit: '''))
if choice == 1:
print(withdraw());
elif choice == 2:
print(deposit());
elif choice == 3:
print(bal());
more = input("Would you like another transaction? (y/n)")

Maybe you need a loop to repeat the choice :
while True:
print('''Current balance: $''',balance)
choice = int(input('''
Please choose an option from the following:
1 - Withdraw
2 - Deposit
3 - Check Balance
4 - Exit: '''))
if choice == 1:
print(withdraw());
elif choice == 2:
print(deposit());
elif choice == 3:
print(bal());
more = input("Would you like another transaction? (y/n)")
if more.lower() != 'y':
print("Goodbay")
break

Related

Loop keeps returning to wrong part [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I'm taking a fundamentals of programming class and we're supposed to be building a menu that calculates BMI and also shows different gym membership options, what I can't figure out is why my menu keeps looping back to the BMI calculator after viewing the membership rates.
this is some of my code:
def mainmenu():
option = int(input("Enter your option: "))
while option != 0:
if option == 1:
try:
print("Calculate BMI")
the_height = float(input("Enter the height in cm: "))
assert the_height > 0
the_weight = float(input("Enter the weight in kg: "))
assert the_weight > 0
the_BMI = the_weight / (the_height/100)**2
except ValueError:
print("Enter height and weight in whole numbers")
print("Your BMI is", the_BMI)
if the_BMI <= 18.5:
print("You are underweight.")
elif the_BMI <= 24.9:
print("You are Normal.")
elif the_BMI <= 29.9:
print("You are overweight.")
else:
print("You are obese.")
check = input("Do you want to quit or start again, enter Y to restart or another to end ?: ")
if check.upper() == "Y":
print("Bye...")
mainmenu()
elif option == 2:
def submenu():
print("Choose your membership type")
print("[1] Bassic")
print("[2] Regular")
print("[3] Premium")
print("[0] Exit to main menu")
loop = True
while loop:
submenu()
option = int(input("Enter your option: "))
if option == 1:
print("Basic Membership")
print("$10 per week, $40 per month")
break
elif option == 2:
print("Regular Membership")
print("$15 per week, $60 per month")
check = input("Do you want to quit or start again, enter Y to restart or another to end ?: ")
if check.upper() == "Y":
submenu()
elif option == 3:
print("Premium Membership")
print("$20 per week, $80 per month")
check = input("Do you want to quit or start again, enter Y to restart or another to end ?: ")
if check.upper() == "Y":
submenu()
elif option == 0:
loop = False
else:
break
else:
print("Invalid option....")
break
mainmenu()
option = int(input("Enter your option: "))
Any suggestions would be helpful, I've been playing around for a while and can't find the solution.
It looks like the reason for this is that you're using option variable to store the value that user provide for both main menu and sub menu.
Instead of this
submenu()
option = int(input("Enter your option: "))
Use
submenu()
submenu_option = int(input("Enter your option: "))
Also replace the option with submenu_option only where you wants to refer to the user selection for the sub menu

How to return the right positive number in python whithou changing the initial variable?

I have create functions to simulate ATM system, when asking the user to make deposit everything goes well but the problem is when the user make a withdraw. If the amount is greater than the balance I have the new balance in negative value. And need some help to correct this behavior.
My functions:
def show_balance(balance):
print(f"Current Balance: ${balance}")
def deposit(balance):
amount = input("Enter amount to deposit: ")
amount = float(amount)
result = balance + amount
return result
def withdraw(balance):
withdraw = input("Enter amount to withdraw: ")
withdraw = float(withdraw)
result = balance - withdraw
return result
Here is when using the functions:
# Display ATM menu to user
while True:
option = input("Choose an option: ")
if option == "1":
account.show_balance(balance)
elif option == "2":
balance = account.deposit(balance)
account.show_balance(balance)
elif option == "3":
balance = account.withdraw(balance)
account.show_balance(balance)
while True:
if balance < 0:
print("You do not have this money?")
break
else:
print("Please, provide a valid option")
Now when adding 50 using option "2" everithing works well. When the withdraw is < the balance (55) it just return a nagative value (-5). I want to print the massage: "You do not have this money?", but also keep the balance to the right amount (50 -55) => balance remain 50 and ask the user to try again.
#AKX -> Based on your precious advice, I have fixed the function doing the following:
def withdraw(balance):
withdraw = input("Enter amount to withdraw: ")
withdraw = float(withdraw)
if withdraw <= balance:
result = balance - withdraw
return result
elif withdraw > balance:
print("You do not have this money?")
return balance

How do i get the code to re run an old path that was already taken?

I am trying to create an ATM that asks for a user name to log in and then for each user have three separate accounts to pick from. Within each of those accounts allow them to deposit, withdraw, and view balance. My problem is I am not very good with lists and I believe that is what is needed. I can not get the code to log in as a new user once I have already gone threw the code. example: I create a user Bob and log in and deposit money. Then I log out of Bob and want to create a new user Tim. when I create Tim it wont let me log it. It will keep giving me the same menu every time I put in Tim.
I believe I need to create a list of users, then a list for each user and I do not understand how to do so. Seeing from my code I just use set values for the funds in each account. Could this be a problem with why the main log in wont allow me to use another user?
user_list = []
data_list = []
index = -1
user_input = 0
user_account = 0
credit_input = 0
checking_input = 0
saving_input = 0
while user_input != 3:
print("1: Login\n2: New User\n3: Exit")
user_input = int(input("Please pick an option: "))
if user_input == 1:
username = input("Login: ")
while username not in user_list:
username = input("No user, try again: ")
index = user_list.index(username)
while user_account != 4:
print("Accounts:\n\n1: Credit\n2: Checking\n3: Savings\n4:Exit ")
user_account = int(input("Please pick an option: "))
if user_account == 1:
credit_funds = 0
while credit_input != 4:
print("1: Deposit")
print("2: Withdraw")
print("3: Credit Account Balance")
print("4: Exit")
credit_input = int(input("Pick an option: "))
if credit_input == 1:
number = int(input("Deposit amount: "))
credit_funds += number
print("Deposit of $", number)
elif credit_input == 2:
number = int(input("Withdraw amount: "))
while number > credit_funds:
print("\nInsufficient Funds")
break
else:
credit_funds -= number
print("\nSuccessful Withdraw of $", number)
elif credit_input == 3:
print("Avalable balance: $", credit_funds)
elif user_account == 2:
checking_funds = 0
while checking_input != 4:
print("1: Deposit")
print("2: Withdraw")
print("3: Checking Account Balance")
print("4: Exit")
checking_input = int(input("Pick an option: "))
if checking_input == 1:
amount = int(input("Deposit amount: "))
checking_funds += amount
print("Deposit of $", amount)
elif checking_input == 2:
amount = int(input("Withdraw amount: "))
while amount > checking_funds:
print("\nInsufficient Funds")
break
else:
checking_funds -= amount
print("\nSuccessful Withdraw of $", amount)
elif checking_input == 3:
print("Avalable balance: $", checking_funds)
elif user_account == 3:
saving_funds = 0
while saving_input != 4:
print("1: Deposit")
print("2: Withdraw")
print("3: Saving Account Balance")
print("4: Exit")
saving_input = int(input("Pick an option: "))
if saving_input == 1:
number3 = int(input("Deposit amount: "))
saving_funds += number3
print("Deposit of $", number3)
elif saving_input == 2:
number3 = int(input("Withdraw amount: "))
while number3 > saving_funds:
print("\nInsufficient Funds")
break
else:
saving_funds -= number3
print("\nSuccessful Withdraw of $", number3)
elif saving_input == 3:
print("Avalable balance: $", saving_funds)
elif user_input == 2:
username = input("Please pick a username: ")
while username in user_list:
username = input("Pick another please: ")
user_list.append(username)
When the user 'logs out' (presses 4 when logged in) you set user_account to 4 (an exit condition). It is never unset after that. Therefore when another user tries to login, the program tests user_account != 4, fails that test, and never enters the while loop (while user_account != 4). I suspect the same will happen for all other exit conditions.
I suggest resetting the value of any inputs to 0 after taking the appropriate action. Alternatively, use while True: and explicitly break when an exit condition is reached.
For example:
while True:
print("1: Login\n2: New User\n3: Exit")
user_input = int(input("Please pick an option: "))
if user_input == 1:
print("Option 1 selected")
# DO SOMETHING
elif user_input == 2:
print("Option 2 selected")
# DO SOMETHING
elif user_input == 3:
break
users={};stop='r'
def login(name,users):
if name in users.keys():
return True
return False
def create():
name=input('please write your name')
users[name]=0
def main(users):
print('Welcome to my ATM\n')
buff=input('SELECT one of the following:\nC)create account\nL)login')
if(buff=='C' or buff=='c'):
create()
elif(buff=='L' or buff=='l'):
name=input('name: ')
buff2=login(name,users)
if(buff2==False):
print('your name is not in account list')
elif(buff2==True):
buff=input('SELECT:\nA)cash report\nD)delete account')
if(buff=='A' or buff=='a'):
print(users[name])
elif(buff=='D' or buff=='d'):
k=users.pop(name)
print('finished! ')
while(stop=='r'):
buff3=input("press 's' to stop ATM otherwise press enter to continue ...")
if(buff3=='s'):
break
main(users)
I wish this works.

Functions and Local Variables in Python

I'm writing an ATM program for a class project, and we're not allowed to use global variables. I used only local variables in my program, but it doesn't work.
def welcome():
print("Welcome to the ATM program!\nThis program allows you to deposit, withdraw, or view your balance!")
def menu():
print("1...Deposit\n2...Withdraw\n3...View Balance")
userChoice = int(input("Please enter your choice now: "))
if userChoice == 1:
def deposit(balance):
deposit = float(input("Please enter the amount you would like to deposit: "))
balance = balance + deposit
elif userChoice == 2:
def withdraw(balance):
withdraw = float(input("Please enter the amount you would like to withdraw: "))
balance = balance + withdraw
else:
def balance(balance):
print("Your balance is", balance)
deposit()
withdraw()
balance()
welcome()
menu()
When I run it, it just ends after I input a choice from the menu without any error messages.
There's no reason to define functions here - just execute that code in the if statements, instead:
def menu(balance):
print("1...Deposit\n2...Withdraw\n3...View Balance")
userChoice = int(input("Please enter your choice now: "))
if userChoice == 1:
deposit = float(input("Please enter the amount you would like to deposit: "))
balance = balance + deposit
elif userChoice == 2:
withdraw = float(input("Please enter the amount you would like to withdraw: "))
balance = balance + withdraw
else:
print("Your balance is", balance)
return balance
...
balance = 0
balance = menu(balance)
The reason nothing is happening is because, with the way your code is now, you're defining the functions but not calling them. Look at your indentation - the calls to withdraw(), deposit(), and balance() are only executed inside the else block. And without any arguments, to boot, which would cause an error if they were executed.

I have to create a basic Bank Menu and I've been having a hard time wrapping my head around it

I'm trying to figure out where to go from here, I know I have to have an accumulator somewhere in there to show the current balance of the the user. I'm not quite sure how to implement it, as well as keeping it persistent when the user wants to do other things like 'withdrawal' and other stuff.
I've also tried to somehow make the program terminate itself, but have been having a hard time figuring out that too. I've even tried taking a break from it and looking at it again just to see if I could come up with something new. Not too sure what else to do.
EDIT: I can see that I've been confusing a few people. This was what I was asked to do: I was asked to create a basic program that displays a bank account menu where each option would do these things: Open Account would greet the new user and gives them the option to make an initial deposit to create the account. Deposit would just update the account's balance. Withdraw would update the account by withdrawing the amount the user would ask to take out of the balance. Balance would just display the user's balance, and exit would quit the program
balance = 0.0000
print("Welcome to Sys Financial Bank! New clients must open a new account to continue properly.")
print("""1) New Account
2) Deposit
3) Withdraw
4) Balance
5) Exit""")
option = int(input("Please input the number corresponding with the option in the menu: "))
if option == 1:
option_1 = float(input("Input initial deposit amount to create account: $"))
balance =+ option_1
elif option == 2:
option_2 = float(input("Input deposit amount: $"))
balance = option_1 + option_2
elif option == 3:
option_3 = float(input("Input withdrawal amount: $"))
balance = option_1 - option_3
elif option == 4:
print("Your current balance is: ", balance)
else:
import sys
sys.exit()
I don't understand what you are trying to do, but i am going to guess.
You want a program that while the user doesn't hit five doesn't exit, right?
The way I'd do it
from sys import exit
balance = 0.0000
print("Welcome to Sys Financial Bank! New clients must open a new account to continue properly.")
print("""1 New Account \n2) Deposit \n3) Withdraw \n4) Balance \n5) Exit""")
try:
while True:
option = int(input("Please input the number corresponding with the option in the menu: "))
if option == 1:
option_1 = float(input("Input initial deposit amount to create account: $"))
balance += option_1
elif option == 2:
option_2 = float(input("Input deposit amount: $"))
balance += option_2
elif option == 3:
option_3 = float(input("Input withdrawal amount: $"))
balance -= option_3
elif option == 4:
print("Your current balance is: ", balance)
elif option == 5:
exit('Have a nice day')
except ValueError:
print('Input a number')
However this does not ensure "New clients must open a new account to continue properly". Not that it matters. but if you must. then execute the option 1 prior to the while

Categories

Resources