Im trying to make an easy interface for a "bank", that is, a function choose() that lets the user choose the action they want, together with input values for the action if they are needed. The options so far included is printing the balance, deposit or withdraw money, and do the interest settlement. But I would like to add another option in the interface that lets the user see the last three changes made. How could I do that? Like for example they choose option 5 and they can see the changes like this:
+6105.0
-500000
+1110000
This is the code I got so far:
balance = 500
intrest_rate = 0.01
def deposit(amount):
global balance
balance= (balance+amount)
print(balance)
deposit(100)
print(balance)
def withdrawl(amount):
global balance
if amount>balance:
print("This amount will make you bankrupt:(")
else:
balance=(balance-amount)
print(f"You withdrew {amount} NOK from your balance , you know have {(balance)} NOK in your account")
withdrawl(150)
print(balance)
def calculating_intrest_rate():
global balance
global intrest_rate
if balance >= 1000000:
intrest_rate = 0.02
elif balance <= 1000000:
intrest_rate = 0.01
calculating_intrest_rate()
print(intrest_rate)
deposit(1000024)
calculating_intrest_rate()
print(intrest_rate)
def intrest_added_to_balance():
global intrest_rate
global balance
multiply = intrest_rate * balance
balance = (multiply+balance)
print(balance)
intrest_added_to_balance()
def choose():
print("""
1 - show balance
2 - deposit
3 - withdraw
4 - intrest settlement
5 - last changes
""")
choice = input("Please choose an action:")
if choice == "1":
print(balance)
elif choice == "2":
x= int(input("Please enter the amount you wish to deposit: "))
print(f"You added {x} NOK to your balance , you know have {(x) + (balance)} NOK in your account")
elif choice == "3":
y= int(input("Please enter the amount you wish to withdraw: "))
withdrawl(y)
elif choice == "4":
balance = balance + int((balance) * intrest_rate)
print (balance)
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
I decided that I would write a code to simulate a betting game, where you guess the number the dice will land on. Eventually, the code became a full-blown simulator, with credit-card withdrawals and different dice types and stakes (don't ask me why there's a 4-sided dice, I doubt it's possible).
Anyway, I kept getting the same error. This is my code:
import random
from random import randint
diea = randint(1,4)
dieb = randint(1,5)
diec = randint(1,6)
jackpot = randint(1,30)
chance = randint (1,30)
cquit = "a"
bal = 10
credcard = 100
withdrawal = 0
deposit = 0
if jackpot == chance:
print("You won the jackpot of 10k!")
bal = bal + 10000
while cquit.lower() == "a":
print ("Your balance is $", bal)
print ("Your credit card balance is $", credcard)
choicea = input("Would you like to deposit / withdraw money - a for deposit, b for withdrawal, anything else to skip: ")
if choicea.lower() == "a":
deposit = int(input("How much would you like to deposit - you have $", bal," on you right now: "))
if deposit > bal:
print ("You do not have enough money - cancelling process")
else:
credcard = deposit + credcard
bal = bal - deposit
print ("Your balance is $", bal)
print ("Your credit card balance is", credcard)
if choicea.lower() == "b":
withdrawal = int(input("How much money would you like to withdraw - you have $", credcard," on your card"))
if withdrawal > credcard:
print ("Your card does not allow overdrafts - cancelling process")
else:
bal = withdrawal + bal
credcard = credcard - withdrawal
print ("Your balance is $", bal)
bet = int(input("How much would you like to bet?: "))
if bet > bal:
print ("You do not have enough money")
else:
diechoice = input("Choose die - A (1-4, x2), B (1-5, x3), C(1-6, x4): ")
if diechoice.lower() == "a":
guess = int(input("What will the die land on?: "))
if guess == diea:
print ("Correct guess - balance doubled")
bal = bal + bet
if guess > 4:
print ("That is above the die's capacity - bet cancelled")
if guess != diea:
print ("Incorrect guess - bet removed from balance")
bal = bal - bet
if diechoice.lower() == "b":
guess = int(input("What will the die land on?: "))
if guess == diea:
print ("Correct guess - tripling balance")
bet = bet*2
bal = bal + bet
if guess > 5:
print ("That is above the die's capacity - bet cancelled")
if guess != dieb:
print ("Incorrect guess - bet removed from balance")
bal = bal - bet
if diechoice.lower() == "c":
guess = int(input("What will the die land on?: "))
if guess == diea:
print ("Correct guess - quadrupling balance")
bet = bet*3
bal = bal + bet
if guess > 6:
print ("That is above the die's capacity - cancelling bet")
if guess != diec:
print ("Incorrect guess - bet removed from balance")
bal = bal - bet
elif diechoice.lower() != "a" and diechoice.lower() != "b" and diechoice.lower() != "c":
print ("Incorrect input - skipping bet")
cquit = input("a to continue, anything else to end: ")
if cquit.lower() == "a":
if bal == 0 and credcard == 0:
print ("ending program - you are bankrupt")
cquit = "b"
if bal > 0:
print ("continuing program")
print ("...")
print ("...")
print ("...")
else:
print ("ending program")
This is the error I get in the code when testing it. It happens when I enter a or b on the first input statement whenever it loops:
Traceback (most recent call last):
File "D:\FAKE NEWS\du.py", line 30, in <module>
withdrawal = int(input("How much money would you like to withdraw - you have $", credcard," on your card"))
TypeError: input expected at most 1 arguments, got 3
I've been reading other reports on the same errors but I'm too bad at Python to understand any of it.
input() is not like print(), you can't give it multiple arguments and expect it to concatenate them automatically in the prompt.
You have to do the string formatting yourself.
withdrawal = int(input("How much money would you like to withdraw - you have $%.2f on your card" % (credcard)))
deposit = int(input("How much would you like to deposit - you have $" + str(bal) + " on you right now: "))
and
withdrawal = int(input("How much money would you like to withdraw - you have $"+ str(credcard) + " on your card"))
You're passing three arguments to input, but it only takes one.
To put a number into a string, use str.format:
prompt = "You have ${} on your card.".format(dollars)
input(prompt)
This inserts the value of dollars as the first "replacement field" ({}), formatting it by calling str. You can add multiple replacement fields, and you can name them like so:
"x is {}, y is {}".format(x, y)
"x is {1}, y is {0}".format(y, x) # note switched arguments
"x is {x} y is {y}".format(x=x, y=y)
If you are using Python 3.6 or later, you can use an f-string instead:
current_balance = 100;
f"You have ${current_balance} on your card."
There are many other things you can do with the string-formatting syntax. This answer provides a quick look at a few of them, if you don't want to read the documentation.
change input line from
deposit = int(input("How much would you like to deposit - you have $", bal," on you right now: "))
to
deposit = int(input("How much would you like to deposit - you have $"+str(bal)+" on you right now: "))
b. Create a program that simulates a simple savings account simulation. First ask the user for the initial balance (must be at least $100 and less than $400,000). As long as the user wants to continue allow them to make deposits and withdrawals – don’t overdraft! When the user is done, output the final balance.
(suggestion…use a menu something like:
1. Deposit
2. Withdrawal
3. Quit )
balance = int(input("Enter initial balance: $ "))
while balance <= 100 or balance >= 400000:
print ("Invalid Amount!")
balance = int(input("Ener valid amount: $ "))
deposit = 0
withdraw = 0
if balance >= 100 and balance <= 400000:
while ans != 3:
print("""
1. Deposit
2. Withdrawal
3. Quit
""")
ans = int(input("What would you like to do? Please enter the appropriate number. "))
if ans == 1:
deposit = int(input("\n How much would you like to deposit: $")
balance = balance + deposit
elif ans == 2:
withdraw = int(input("\n How much would you like to withdraw: $")
if (balance - withdraw) < 0
withdraw = int(input("\n Tried to withdraw too much! How much would you like to withdraw: $")
balance = balance - withdraw
elif ans == 3:
print("Your final balance is %d" %balance.)
You're missing a ) on the previous line, you want:
if ans == 1:
deposit = int(input("\n How much would you like to deposit: $"))
and an other one:
withdraw = int(input("\n How much would you like to withdraw: $"))
and then a tailing ::
if (balance - withdraw) < 0:
and another ) one further on:
if (balance - withdraw) < 0
withdraw = int(input("\n Tried to withdraw too much! How much would you like to withdraw: $"))
and then a space after your % (and get rid of the .):
print("Your final balance is %d" % balance)
You're missing a ')' on line 16.
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