I want it so that my current balance will be the as the amount withdrawn for the next loop.
Enter pin:123 1 – Balance Inquiry 2 – Withdraw 3 – Deposit X – Exit Enter your choice:2 Hi name1.
Your current balance is 50000
Enter amount to withdraw: 400 Transaction Successful
Your current balance is 49600
Enter pin:123 1 – Balance Inquiry 2 – Withdraw 3 – Deposit X – Exit Enter your choice:2 Hi name1.
Your current balance is 50000 *** Problem ***
Enter amount to withdraw:
This is currently my code. (sorry for the messy code as I am a beginner)
pin = [123, 456, 789]
balance = [50000, 2000, 500]
name = ["name1", "name2", "name3"]
def main():
while True:
pin_input = input('Enter pin:')
try:
n = int(pin_input)
except:
break
if n in pin:
print('1 – Balance Inquiry')
print('2 – Withdraw')
print('3 – Deposit')
print('X – Exit')
choice = input('Enter your choice:')
c = int(choice)
if choice == 'X':
print('Thank you for banking with us!')
break
else:
pol = pin.index(n)
if c == 1:
print(f'Hi {name[pol]}.')
print(f'Your current balance is {balance[pol]} ')
elif c == 2:
print(f'Hi {name[pol]}.')
print(f'Your current balance is {balance[pol]} ')
withdraw = int(input('Enter amount to withdraw: '))
if withdraw > balance[pol]:
print('Not enough amount')
else:
difference = balance[pol] - withdraw
print('Transaction Successful')
print(f'Your current balance is {difference}')
elif c == 3:
print(f'Hi {name[pol]}.')
print(f'Your current balance is {balance[pol]} ')
deposit = int(input('Enter amount to deposit: '))
sums = deposit balance[pol]
print('')
print(f'Your current balance is {sums}')
main()
CodePudding user response:
welcome to Python. I see what is your problem. When you handle the withdrawal you create a new variable and performed the subtraction you just displayed the new result and never updated it. So to solve it you need to replace this code:
difference = balance[pol] - withdraw
print(f'Transaction Successful.\nYour current balance is {difference}')
with:
balance[pol] -= withdraw
print(f'Transaction Successful.\nYour current balance is {balance[pol]}')
I took the liberty to edit your code a bit and make it more "professional" but also so that you could read and understand it (I added comments for you to read).
pin = [123, 456, 789]
balance = [50000, 2000, 500]
name = ["name1", "name2", "name3"]
def main():
while True:
try:
pin_input = int(input('Enter pin:'))
except ValueError: #It's bad practice to leave it just as "except".
break
if pin_input in pin:
print('1 – Balance Inquiry')
print('2 – Withdraw')
print('3 – Deposit')
print('X – Exit')
choice = input('Enter your choice:')
#As you can see, I have removed the conversion because no one will care if it is int or str, it's happening behind the scene.
if choice == 'X':
print('Thank you for banking with us!')
break
#You don't need the else statement because if the choice would be 'X' it would automatically exit.
pol = pin.index(pin_input)
if choice == '1':
print(f'Hi {name[pol]}.\nYour current balance is {balance[pol]}') #Using \n to downline instead of using two prints.
elif choice == '2':
print(f'Hi {name[pol]}.\nYour current balance is {balance[pol]}') #Using \n to downline instead of using two prints.
withdraw = int(input('Enter amount to withdraw: ')) #Assuming the user will write an integer.
if withdraw > balance[pol]:
print('Not enough amount')
break # Let's just add that here (easier to read) and we don't need the else statement anymore.
balance[pol] -= withdraw
print(f'Transaction Successful.\nYour current balance is {balance[pol]}')
elif choice == '3':
print(f'Hi {name[pol]}.\nYour current balance is {balance[pol]}')#Using \n to downline instead of using two prints.
deposit = int(input('Enter amount to deposit: ')) #Assuming the user will write an integer. the user
sums = deposit balance[pol]
print(f'\nYour current balance is {sums}') #\n instead of a whole print function.
if __name__ == "__main__": #Use this script as the main script.
main()
Nice work, keep up with this good job!
Also, I want to add my own way of creating an ATM machine. I hope that one day when you will learn and have more knowledge you would open this again and try to understand this. (This code will work only in py-3.10 or higher)
Code:
class ATM:
def __init__(self, pin) -> None:
self.pin = pin
def Balance(self) -> str:
return f"{data[self.pin][1]}, Your balance is: {data[self.pin][0]}"
def Withdraw(self) -> str:
try:
withdraw = int(input('Enter amount to withdraw: '))
if withdraw > data[self.pin][0]:
return f"{data[self.pin][1]}, Looks like you can't withdraw {withdraw}$ due to lower balance.\nYou can withdraw up to {data[self.pin][0]}$."
data[self.pin][0] -= withdraw
except ValueError:
return f"{data[self.pin][1]}, Looks like there was an error with your request to withdraw. Please try again."
return f"{data[self.pin][1]}, You've successfully withdrawn {withdraw}$. Your remaining balance is: {data[self.pin][0]}$."
def Deposit(self) -> str:
try:
deposit = int(input('Enter amount to Deposit: '))
data[self.pin][0] = deposit
except ValueError:
return f"{data[self.pin][1]}, Looks like there was an error with your request to deposit. Please try again."
return f"{data[self.pin][1]}, You've deposited {deposit}$ into your account. Your balance right now is {data[self.pin][0]}"
if __name__ == "__main__":
data = {
123 : [50000, "name1"],
456 : [2000, "name2"],
789 : [500, "name3"]
}
options = {
1 : "Balance Inquiry",
2 : "Withdraw",
3 : "Deposit",
'X' : "Exit"
}
running = True
while running:
try:
pin = int(input("Enter your pin: "))
if pin in data:
user = ATM(pin)
else:
print(f"User {pin} Doesn't exist. Please check your input and try again.")
continue
for key, value in options.items():
print(f"Press '{key}' for {value}")
while True:
action = input("Please enter your action: ")
match action:
case '1':
print(user.Balance())
break
case '2':
print(user.Withdraw())
break
case '3':
print(user.Deposit())
break
case 'X' | 'x':
running = False
break
case _:
print("This action doesn't exist. Please try again.")
continue
except ValueError:
print("There is an error in the given pin. Please try again.")
continue