Answer:
STARTING_ITEMS_IN_INVENTORY = 20
num_items = STARTING_ITEMS_IN_INVENTORY
# Enter your code here
while num_items > 0:
print("We have " + str(num_items) + " items in inventory")
toBuy = int(input("How many would you like to buy?"))
if toBuy > num_items:
print("There is not enough in inventory")
print("Now we have " + str(num_items) + " left")
else: # ok to sell
num_items = num_items - toBuy # update
if num_items > 0: # only for printing
print("Now we have " + str(num_items) + " left")
print("All out!")
Explanation:
This allows Python to store the number of items in inventory after each purchase by subtracting how much is bought with the toBuy function by how much is left. This continues until num_items is no longer greater than zero. If what’s toBuy goes above the # of items left in inventory, then the “if toBuy > num_items” segment of the code comes into play by telling the user there’s not enough and re telling them how much is left and how much they’d like to buy. When the items in inventory is out, meaning the exact amount that’s left is purchased, then Python prints “All out!”