Follow the money in an account, one step at a time.
What to build
A program in which one variable changes several times, and is shown after each change.
Requirements
- Read the starting balance, a deposit and a withdrawal.
- Print
Start: S. - Add the deposit to the balance and print
After deposit: B. - Take away the withdrawal and print
After withdrawal: B. - Print
Change: C, the final balance minus the starting balance.
Input
Three lines, each with one whole number.
Output
Four lines, as in the example.
Constraints
Each number is from 0 to 1000000. The balance may go below zero.
Example
Input:
100
50
30
Output:
Start: 100
After deposit: 150
After withdrawal: 120
Change: 20
Why it works
An assignment such as balance = balance + deposit uses the old value to make the new one. Keeping the starting value in its own variable means it is still there at the end.
Examples
Example 1 · Input
100 50 30
Expected output
Start: 100 After deposit: 150 After withdrawal: 120 Change: 20
Example 2 · Input
0 10 25
Expected output
Start: 0 After deposit: 10 After withdrawal: -15 Change: -15
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use one variable, balance, and give it a new value at each step.
Hint 2
Print after each change, so that you see the value at that moment.
Solution
Show one correct solution (try it yourself first)
start = int(input())
deposit = int(input())
withdrawal = int(input())
balance = start
print("Start:", balance)
balance = balance + deposit
print("After deposit:", balance)
balance = balance - withdrawal
print("After withdrawal:", balance)
print("Change:", balance - start)