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_transatiction == "Deposit":
balance=balance+amount
elif type_of_transatiction == "Withdrawal":
balance=balance-amount
else:
print("Invalid Transaction.")
All Answers 1
Answered by
GPT-5 mini
AI
Here is a corrected, slightly improved version and a short explanation of the fixes:
Fixed code:
balance = 1000
type_of_transaction = input("Deposit or Withdrawal: ").strip().lower()
try:
amount = int(input("Enter amount: $"))
if amount < 0:
raise ValueError
except ValueError:
print("Invalid amount.")
else:
if type_of_transaction == "deposit":
balance += amount
print(f"Deposited ${amount}. New balance: ${balance}")
elif type_of_transaction == "withdrawal":
if amount > balance:
print("Insufficient funds.")
else:
balance -= amount
print(f"Withdrew ${amount}. New balance: ${balance}")
else:
print("Invalid transaction.")
What I fixed and improved:
- Corrected the variable-name typo (type_of_transatiction -> type_of_transaction).
- Corrected the prompt spelling ("Withdrawl" -> "Withdrawal").
- Made transaction input case-insensitive with .strip().lower().
- Added input validation for amount (non-integer and negative checks).
- Handled insufficient funds on withdrawal.
- Printed the updated balance after a successful transaction.
If you want, I can show a version that loops for multiple transactions or that persists balance between runs.
Fixed code:
balance = 1000
type_of_transaction = input("Deposit or Withdrawal: ").strip().lower()
try:
amount = int(input("Enter amount: $"))
if amount < 0:
raise ValueError
except ValueError:
print("Invalid amount.")
else:
if type_of_transaction == "deposit":
balance += amount
print(f"Deposited ${amount}. New balance: ${balance}")
elif type_of_transaction == "withdrawal":
if amount > balance:
print("Insufficient funds.")
else:
balance -= amount
print(f"Withdrew ${amount}. New balance: ${balance}")
else:
print("Invalid transaction.")
What I fixed and improved:
- Corrected the variable-name typo (type_of_transatiction -> type_of_transaction).
- Corrected the prompt spelling ("Withdrawl" -> "Withdrawal").
- Made transaction input case-insensitive with .strip().lower().
- Added input validation for amount (non-integer and negative checks).
- Handled insufficient funds on withdrawal.
- Printed the updated balance after a successful transaction.
If you want, I can show a version that loops for multiple transactions or that persists balance between runs.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.