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. A sample run is below.

(CodeHS, PYTHON)

Respuesta :

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!”

Following are the code to the given question:

Program Explanation:

  • Defining a variable "INVENTORY_ITEMS" that hold an integer value.
  • Defining a variable "num" that holds "INVENTORY_ITEMS" value.
  • In the next step, a while loop is declared that checks "num" value greater than 0.
  • Inside the loop, "num and To_Buy" is declared and in the To_Buy it inputs value by user-end.
  • In the next step, conditional statement is used that check "To_Buy" value and prints its value.
  • At the last another conditional statement is used that checks num value, and print its value.

Program:

INVENTORY_ITEMS = 20#defining a variable INVENTORY_ITEMS that hold integer value

num= INVENTORY_ITEMS#defining a num variable that holds INVENTORY_ITEMS value  

while num > 0:#defining a while loop that check num value greater than 0

   print("We have " + str(num) + " items in inventory")#print num value with message

   To_Buy = int(input("How many would you like to buy?"))#defining To_Buy variable that inputs value

   if To_Buy > num:#defining if block that check To_Buy value greater than num value

       print("There is not enough in inventory")#print message

       print("Now we have " + str(num) + " left")#print num value with message

   else: # defining else block

       num= num - To_Buy#using num variable that decreases num by To_Buy

if num> 0: #use if to check num greater than 0

   print("Now we have " + str(num) + " left")#print num value with message

print("All out!")#print message

Output:

Please find the attached file.

Learn more:

brainly.com/question/18634688

Ver imagen codiepienagoya

Otras preguntas