119. Python Program to Manage a Shop Inventory
HardLooping
Overview
Put the whole chapter together: add, change and delete pairs, check keys with in, and loop over the result.
Problem Statement
The shop starts with an empty stock. Read commands, one per line, until a line says done. Each command is a word, an item and a whole number, separated by spaces:
add apple 5: add 5 to the stock of apple (a new item starts at 0). PrintAdded 5 apple.sell apple 2: if apple isn't in stock, printNo apple in stock. If there are fewer than 2, printOnly 1 apple left(with the real number) and sell nothing. Otherwise take 2 away and printSold 2 apple. When an item's stock reaches 0, remove it from the stock.
After done, print Inventory:, then every item and its quantity in the format apple 7, in the order the items were first added, then Total items: and the sum of all quantities. For example, for the input below:
Added 5 apple
Added 10 pen
Sold 2 apple
Only 10 pen left
No kiwi in stock
Added 4 apple
Inventory:
apple 7
pen 10
Total items: 17
Use input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.
Input
One value per line, in this order. Example:
add apple 5
add pen 10
sell apple 2
sell pen 12
sell kiwi 1
add apple 4
done
Output
The output for that example:
Added 5 apple
Added 10 pen
Sold 2 apple
Only 10 pen left
No kiwi in stock
Added 4 apple
Inventory:
apple 7
pen 10
Total items: 17
The tests try 4 different inputs.