In: Computer Science
Write a Python script that: Ask the customer for their current bank balance. One thing you dont have to worry about is whether or not the user enters a negative or positive amount.. Ask the customer if they wish to deposit ('d') or withdraw ('w') from that amount. Ask them to enter the amount of money they are either depositing or withdrawing. This has to be a positive numeric value. If it is negative, you should print an error message and nullify the transaction (do nothing with their balance, i.e. their final balance will be the same as starting). Add or subtract that amount to/from their balance.
Explanation:
Here is the code which takes balance from the user.
Then it asks for the option whether the user wants to withdraw or deposit the amount.
Then according to that option, amount is asked and the balance is updated.
Code:
balance = float(input("Enter current balance: "))
option = input("Enter option-> 'w' to withdraw or 'd' to deposit: ")
if option=='w':
withdraw_amount = float(input("Enter amount to withdraw: "))
if withdraw_amount>=0:
print("Starting balance:", balance)
balance = balance - withdraw_amount
print("Final balance:", balance)
else:
print("Amount cannot be negative. Transaction cancelled.")
elif option=='d':
deposit_amount = float(input("Enter amount to withdraw: "))
if deposit_amount>=0:
print("Starting balance:", balance)
balance = balance + deposit_amount
print("Final balance:", balance)
else:
print("Amount cannot be negative. Transaction cancelled.")
output:
please upvote if you found this helpful!
please comment if you need any help!