4.10.4 Inventory CodeHS Answers

We thoroughly check each answer to a question to provide you with the most correct answers. Found a mistake? Tell us about it through the REPORT button at the bottom of the page. Ctrl+F (Cmd+F) will help you a lot when searching through such a large set of questions.

4.10.4: Inventory CodeHS general answer I see across the web:

4.10.4: Inventory | CodeHS

# Enter your code here

while num > 0:
print(“We have ” + str(num) + ” items in inventory.”)
To_Buy = int(input(“How many would you like to buy? “))
if To_Buy > num:
print(“There is not enough in inventory for that purchase.”)

num = num – To_Buy
if num > 0:
print(“Now we have ” + str(num) + ” left” )
print(“All out!”)

The provided code snippet for managing an inventory system in Python has a few issues that must be addressed for it to work correctly. Let me provide a corrected version of the code for you:


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

if to_buy > num_items:
    print("There is not enough in inventory for that purchase.")
    num_items = num_items - to_buy
    if num_items > 0:
        print("Now we have " + str(num_items) + " items left.")
        print("All out!")
# Note: The program will exit the loop and end when num_items reaches 0 or below.

Changes made:

  1. Removed redundant variables: The initial code declared STARTING_ITEMS_IN_INVENTORY and INVENTORY_ITEMS with the same value. I’ve removed INVENTORY_ITEMS to simplify the code.
  2. Corrected indentation: The original code had incorrect indentation, which is crucial in Python for defining the scope of loops and conditionals.
  3. Variable Naming: Changed To_Buy to to_buy to follow Python’s naming convention (snake_case for variables).
  4. Loop Exit Condition: Added a check to print “All out!” only after the inventory becomes zero, and moved it outside the else clause to ensure it’s printed when inventory runs out.

This code will now correctly manage an inventory, allowing users to buy items until the inventory runs out.

Was this helpful?

Quizzma Team
+ posts

The Quizzma Team is a collective of experienced educators, subject matter experts, and content developers dedicated to providing accurate and high-quality educational resources. With a diverse range of expertise across various subjects, the team collaboratively reviews, creates, and publishes content to aid in learning and self-assessment.
Each piece of content undergoes a rigorous review process to ensure accuracy, relevance, and clarity. The Quizzma Team is committed to fostering a conducive learning environment for individuals and continually strives to provide reliable and valuable educational resources on a wide array of topics. Through collaborative effort and a shared passion for education, the Quizzma Team aims to contribute positively to the broader learning community.

Leave a Comment

Your email address will not be published. Required fields are marked *