258. Python Program to Make a Wallet App with Transfers
HardPhase 3 · Challenge
This one takes a few steps, but you know every tool it uses: a dictionary, a while loop, split() and if. Try a small example on paper first.
Overview
Phone wallet apps let friends send money to each other in seconds. Behind the screen, the app keeps a balance for each person and checks every transfer before it moves anything.
Problem Statement
Aiko is building a wallet app for friends. Each friend has a wallet with some coins. Your program reads one command per line, until a line says end.
The commands are:
create <name> <amount>: make a new wallet with that many coins. PrintCreated <name> with <amount> coins.send <from> <to> <amount>: move coins from one wallet to another. PrintSent <amount> coins from <from> to <to>.balance <name>: print<name>: <coins> coins.
A send must pass 4 checks, in this order. Print only the first error it finds, and move nothing:
- The sender has no wallet: print
Error: unknown user <from>. - The receiver has no wallet: print
Error: unknown user <to>. - The amount is 0 or less: print
Error: amount must be positive. - The amount is more than the sender's coins: print
Error: not enough coins.
A balance for a name with no wallet prints Error: unknown user <name>.
After end, print Balances:, then one line <name>: <coins> coins for every wallet, in the order the wallets were created.
Examples
Example 1. Input:
create Aiko 100
create Ben 20
send Aiko Ben 30
send Ben Chen 5
balance Ben
end
gives:
Created Aiko with 100 coins
Created Ben with 20 coins
Sent 30 coins from Aiko to Ben
Error: unknown user Chen
Ben: 50 coins
Balances:
Aiko: 70 coins
Ben: 50 coins
Why: Aiko sends 30 coins, so Aiko has 70 and Ben has 50. Chen has no wallet, so the second send moves nothing.
Example 2. Input:
create Omar 10
create Lina 0
send Omar Lina 0
send Lina Omar 5
send Omar Lina -3
send Omar Lina 10
balance Lina
end
gives:
Created Omar with 10 coins
Created Lina with 0 coins
Error: amount must be positive
Error: not enough coins
Error: amount must be positive
Sent 10 coins from Omar to Lina
Lina: 10 coins
Balances:
Omar: 0 coins
Lina: 10 coins
Why: 0 and -3 are not positive amounts. Lina has 0 coins, so Lina cannot send 5. Omar can send all 10 coins, because 10 is not more than 10.
Input
One command per line, in the formats above, then a last line end. Names are single words with no spaces. Every amount is a whole number, and it can be 0 or negative in a send. A create always uses a new name and an amount of 0 or more. Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Output
One line for each command, then Balances: and one line per wallet. With no wallets at all, print only Balances:. The tests try 7 different inputs, also a send of exactly all the coins, a send where both names are unknown (print the sender), and an input with only end.
Try it first. The solution opens when you solve this question, or after 3 tries with Submit.