Quizzma Latest Questions

Write a program that keeps track of inventory

Anonymous

Write a program that keeps track of inventory. The program starts with a predefined number of items in inventory. While there are still items available, the user can input how many items they want to buy. If there are enough items, the purchase is completed, and the inventory is updated. If not, display a message saying there isn’t enough inventory. Once the inventory reaches zero, print “All out!”.




Related Questions

Leave an answer

Leave an answer

2 Answers

  1. STARTING_ITEMS_IN_INVENTORY = 20

    num_items = STARTING_ITEMS_IN_INVENTORY

    while num_items > 0:
    print(“We have ” + str(num_items) + ” items in inventory.”)
    buy_num = int(input(“How many would you like to buy? “))

    if buy_num <= num_items:
    num_items -= buy_num
    print(“Now we have ” + str(num_items) + ” left.”)
    else:
    print(“There is not enough in inventory for that purchase.”)

    print(“All out!”)

  2. Here’s a simple Python program that tracks inventory:

    
    

    # Define initial inventory

    inventory = 100 # Predefined number of items in inventory

    while inventory > 0:

    print(f"Current inventory: {inventory}")

    try:

    # Ask user for the number of items they want to buy

    items_to_buy = int(input("Enter the number of items to buy: "))

    # Check if there are enough items in inventory

    if items_to_buy <= inventory:

    inventory -= items_to_buy # Update inventory

    print(f"Purchase completed. {items_to_buy} items bought.")

    else:

    print("There isn't enough inventory.")

    except ValueError:

    print("Please enter a valid number.")

    # Notify the user when inventory is empty

    if inventory == 0:

    print("All out!")

    ### Explanation:
    1. Initial Inventory: The program starts with a predefined number of items (100 in this case).
    2. Loop: It runs a loop while there are items in the inventory.
    3. User Input: It prompts the user to enter how many items they want to buy.
    4. Check Inventory: If there are enough items, it updates the inventory; otherwise, it displays a message.
    5. End of Inventory: When the inventory reaches zero, it prints “All out!”.

    Feel free to ask more questions or check the extended services for further help!