Asked by dave
# This program simulates a single transaction -
# either a deposit or a withdrawal - at a bank.
balance = 1000
type_of_transaction = input("Deposit or Withdrawl:")
amount=int(input("Enter Amount: $"))
if type_of_transaction == "Deposit":
balance=balance+amount
elif type_of_transaction == "Withdrawal":
balance=balance-amount
else:
print("Invalid Transaction.")
if balance<0:
print("You cannot have a negative balance!")
else:
print(f"Final balance: ${balance}")
All Answers 1
Answered by
GPT-5 mini
AI
Nice start — a few things to clean up:
Problems in the original
- Typo and case-sensitivity: your prompt says "Withdrawl" while you check for "Withdrawal", and you compare strings exactly so "deposit" or "withdraw" will be rejected.
- No validation for non-numeric or negative amounts (ValueError if user types letters).
- You allow a withdrawal that makes the balance negative and only then print a message — better to prevent the withdrawal if funds are insufficient.
- Using float for money can lead to rounding errors (optional improvement: use Decimal).
Here are two improved examples.
Simple, robust version (using float):
```
balance = 1000.0
t = input("Deposit or Withdrawal: ").strip().lower()
amount_str = input("Enter amount: $").strip()
try:
amount = float(amount_str)
if amount <= 0:
print("Amount must be greater than 0.")
else:
if t in ("deposit", "d"):
balance += amount
print(f"Final balance: ${balance:.2f}")
elif t in ("withdrawal", "withdraw", "w"):
if amount > balance:
print("Insufficient funds. Transaction cancelled.")
else:
balance -= amount
print(f"Final balance: ${balance:.2f}")
else:
print("Invalid transaction type.")
except ValueError:
print("Invalid amount entered.")
```
More accurate money handling (use Decimal):
```
from decimal import Decimal, InvalidOperation, getcontext
getcontext().prec = 10
balance = Decimal("1000.00")
t = input("Deposit or Withdrawal: ").strip().lower()
amount_str = input("Enter amount: $").strip()
try:
amount = Decimal(amount_str)
if amount <= 0:
print("Amount must be greater than 0.")
else:
if t in ("deposit", "d"):
balance += amount
print(f"Final balance: ${balance:.2f}")
elif t in ("withdrawal", "withdraw", "w"):
if amount > balance:
print("Insufficient funds. Transaction cancelled.")
else:
balance -= amount
print(f"Final balance: ${balance:.2f}")
else:
print("Invalid transaction type.")
except InvalidOperation:
print("Invalid amount entered.")
```
If you want, I can:
- Add a loop to allow multiple transactions,
- Persist the balance between runs (file or database),
- Show a transaction history,
- Or adapt this into a small CLI menu. Which would you like?
Problems in the original
- Typo and case-sensitivity: your prompt says "Withdrawl" while you check for "Withdrawal", and you compare strings exactly so "deposit" or "withdraw" will be rejected.
- No validation for non-numeric or negative amounts (ValueError if user types letters).
- You allow a withdrawal that makes the balance negative and only then print a message — better to prevent the withdrawal if funds are insufficient.
- Using float for money can lead to rounding errors (optional improvement: use Decimal).
Here are two improved examples.
Simple, robust version (using float):
```
balance = 1000.0
t = input("Deposit or Withdrawal: ").strip().lower()
amount_str = input("Enter amount: $").strip()
try:
amount = float(amount_str)
if amount <= 0:
print("Amount must be greater than 0.")
else:
if t in ("deposit", "d"):
balance += amount
print(f"Final balance: ${balance:.2f}")
elif t in ("withdrawal", "withdraw", "w"):
if amount > balance:
print("Insufficient funds. Transaction cancelled.")
else:
balance -= amount
print(f"Final balance: ${balance:.2f}")
else:
print("Invalid transaction type.")
except ValueError:
print("Invalid amount entered.")
```
More accurate money handling (use Decimal):
```
from decimal import Decimal, InvalidOperation, getcontext
getcontext().prec = 10
balance = Decimal("1000.00")
t = input("Deposit or Withdrawal: ").strip().lower()
amount_str = input("Enter amount: $").strip()
try:
amount = Decimal(amount_str)
if amount <= 0:
print("Amount must be greater than 0.")
else:
if t in ("deposit", "d"):
balance += amount
print(f"Final balance: ${balance:.2f}")
elif t in ("withdrawal", "withdraw", "w"):
if amount > balance:
print("Insufficient funds. Transaction cancelled.")
else:
balance -= amount
print(f"Final balance: ${balance:.2f}")
else:
print("Invalid transaction type.")
except InvalidOperation:
print("Invalid amount entered.")
```
If you want, I can:
- Add a loop to allow multiple transactions,
- Persist the balance between runs (file or database),
- Show a transaction history,
- Or adapt this into a small CLI menu. Which would you like?
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.