livepython.net

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). Print Added 5 apple.
  • sell apple 2: if apple isn't in stock, print No apple in stock. If there are fewer than 2, print Only 1 apple left (with the real number) and sell nothing. Otherwise take 2 away and print Sold 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.

Learn this firstChapter 13 · Loop Through a Dictionary in Python
solution.py
# write your code here

EXPECTED OUTPUT

1Added 5 apple
2Added 10 pen
3Sold 2 apple
4Only 10 pen left
5No kiwi in stock
6Added 4 apple
7Inventory:
8apple 7
9pen 10
10Total items: 17

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.