305,961 views
12 votes
12 votes
Write a program that keeps track of a simple inventory for a store. While there are still items left in the inventory, ask the user how many items they would like to buy. Then print out how many are left in inventory after the purchase. You should use a while loop for this problem.

We have 20 items in inventory. How many would you like to buy? 4 Now we have 16 left. We have 16 items in inventory. How many would you 11 like to buy? 2 Now we have 14 left. We have 14 items in inventory. How many would you like to buy? 8 Now we have 6 left. We have 6 items in inventory. How many would you like to buy? 5 Now we have 1 left. We have 1 items in inventory. How many would you like to buy? 1 A]l out!

Write a program that keeps track of a simple inventory for a store. While there are-example-1
User Kaylum
by
2.7k points

1 Answer

12 votes
12 votes

Answer:

inventory = 20

while inventory > 0:

print("We have", inventory, "items in inventory.")

buy = int(input("How many would you like to buy? "))

inventory -= buy

if inventory != 0:

print("Now we have", inventory, "left.")

else:

break

print("All out!")

User Ogun
by
2.9k points