Python Exercise 16 Problem:
In this Python exercise, write a Python program computes the amount of a banking account transaction based on a transaction from the user input selection. The user input whether the transaction is a deposit or withdraw. Obviously this is not a real bank transaction so this exercise is to keep it simple by just creating transaction types of deposit and withdraw.
Exercise Requirements:
- Prompt user for numerical input of 1 for deposit and 2 for withdraw.
- Print the deposit amount.
- Print a random Transaction number.
Solution Python Exercise 16
Python Code Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import random amount = 100 loggedOn = True while loggedOn: selection = int(input("Select 1 for Deposit, 2 for Withdraw or 3 for Exit: ")) if not selection: break if selection == 1: deposit = float(input("How much will you deposit? ")) amount += deposit print(f"Deposit in the amount of ${format(deposit, '.2f')} ") print(f"Bank account balance ${format(amount, '.2f')} ") elif selection == 2: withdraw = float(input("How much will you withdraw? ")) amount -= withdraw print(f"Withdraw in the amount of ${format(withdraw, '.2f')} ") print(f"Bank account balance ${format(amount, '.2f')} ") else: loggedOn = False print("Transaction number: ", random.randint(10000, 1000000)) |
User Input for Deposit:
1 2 3 | Select 1 for Deposit, 2 for Withdraw or 3 for Exit: 1 How much will you deposit? 6 |
Output for Deposit:
1 2 | Deposit in the amount of $6.00 Bank account balance $106.00 |
User Input for Withdraw:
1 2 3 | Select 1 for Deposit, 2 for Withdraw or 3 for Exit: 2 How much will you withdraw? 80 |
Output for Withdraw:
1 2 | Withdraw in the amount of $80.00 Bank account balance $26.00 |
User Input for Exit with Transaction Number:
1 | Select 1 for Deposit, 2 for Withdraw or 3 for Exit: 3 |
Output for Exit with Transaction Number:
1 | Transaction number: 126757 |
As you can see with the withdraw transaction, if you were to withdraw anything over 100, it would show a negative balance. If you wanted to build a better program with logic to prevent a user from withdrawing anything over the amount, the below is possible way to do it.
Additional Solution Python Exercise 16
The below solution prevents a user from over drawing their account when trying to perform a withdraw transaction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import random amount = 100 loggedOn = True while loggedOn: selection = int(input("\nSelect 1 for Deposit, 2 for Withdraw or 3 for Exit: ")) if not selection: break if selection == 1: deposit = float(input("\nHow much will you deposit? ")) amount += deposit print(f"\nDeposit in the amount of ${format(deposit, '.2f')} ") print(f"Bank account balance ${format(amount, '.2f')} ") elif selection == 2: withdraw = float(input("\nHow much will you withdraw? ")) if amount >= withdraw: amount -= withdraw print(f"\nWithdraw in the amount of ${format(withdraw, '.2f')} ") print(f"Bank account balance ${format(amount, '.2f')} ") else: print("\nThis transaction can not be processed.") print("Withdraw amount is greater than bank account amount. ") else: loggedOn = False print("\nTransaction number: ", random.randint(10000, 1000000)) |
User Input for Withdraw:
1 2 3 | Select 1 for Deposit, 2 for Withdraw or 3 for Exit: 2 How much will you withdraw? 300 |
Output for Withdraw:
1 2 | This transaction can not be processed. Withdraw amount is greater than bank account amount. |