0% found this document useful (0 votes)
7 views2 pages

Cli Gen Theory

This document provides an introduction to Markdown formatting, including headings, lists, emphasis, links, images, and code blocks. It also includes a Python code example for an inventory management system, detailing functions for adding, viewing, and removing items from an inventory. The document serves as a basic guide for users to understand and implement Markdown and Python coding practices.

Uploaded by

copeyic220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Cli Gen Theory

This document provides an introduction to Markdown formatting, including headings, lists, emphasis, links, images, and code blocks. It also includes a Python code example for an inventory management system, detailing functions for adding, viewing, and removing items from an inventory. The document serves as a basic guide for users to understand and implement Markdown and Python coding practices.

Uploaded by

copeyic220
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Welcome to Markdown!

This is a basic example to help you get started with Markdown formatting.

Headings
Use # for headings. Add more # for smaller headings:

# creates the largest heading


## creates a medium heading
### creates a smaller heading

Lists
Unordered List

Item 1
Item 2
Sub-item 2.1
Sub-item 2.2

Ordered List
1. First item
2. Second item
3. Third item

Emphasis
Bold text: **Bold**
Italic text: *Italic*
Strikethrough: ~~Strikethrough~~

Links
Click here to visit Google

Images

Code Blocks
Inline code: Use `backticks` for inline code
Block of code:
inventory = {} # A dictionary to store inventory items (item_name: quantity)

def add_item(item_name, quantity): """ Adds a new item to the inventory or updates its quantity if it already exists.

What: This function takes an item name (string) and a quantity (integer) as input.
If the item is not in the inventory, it's added with the given quantity.
If the item already exists, its quantity is increased by the given amount.

Theory: This function utilizes a dictionary to manage the inventory. Dictionaries in Python
store key-value pairs. Here, the item name serves as the key, and the current
stock quantity serves as the value. Checking if a key exists in a dictionary
is an efficient operation. Updating the value associated with a key or adding
a new key-value pair are fundamental dictionary operations.
"""
if item_name in inventory:
inventory[item_name] += quantity # Update existing quantity
print(f"Added {quantity} more units of '{item_name}'. Total: {inventory[item_name]}")
else:
inventory[item_name] = quantity # Add new item to inventory
print(f"'{item_name}' added to inventory with quantity: {quantity}")
def view_inventory(): """ Displays the current inventory, showing each item and its quantity.

What: This function iterates through the 'inventory' dictionary and prints the name
and quantity of each item currently in stock.

Theory: Iterating through a dictionary in Python allows access to both the keys (item names)
and the values (quantities). A simple 'for' loop combined with the `.items()`
method of the dictionary provides this functionality. Formatting the output
makes the inventory information readable to the user.
"""
if not inventory:
print("Inventory is empty.")
return
print("\n--- Current Inventory ---")
for item, quantity in inventory.items():
print(f"{item}: {quantity}")
print("-------------------------")

def remove_item(item_name, quantity): """ Removes a specified quantity of an item from the inventory.

What: This function takes an item name and a quantity to remove as input.
It checks if the item exists and if there's enough stock to remove.

Theory: Before removing items, it's crucial to verify the item's existence in the
inventory and ensure that the requested removal quantity does not exceed the
current stock. Conditional statements ('if', 'elif', 'else') are used to handle
these different scenarios. Updating the dictionary value (reducing quantity)
and potentially removing the item entirely (if the quantity becomes zero or negative,
though this implementation keeps it at zero) are key dictionary manipulations.
"""
if item_name not in inventory:
print(f"Item '{item_name}' not found in inventory.")
return
if inventory[item_name] < quantity:
print(f"Not enough stock of '{item_name}'. Available: {inventory[item_name]}")
return
inventory[item_name] -= quantity
print(f"Removed {quantity} units of '{item_name}'. Remaining: {inventory[item_name]}")
if inventory[item_name] == 0:
print(f"'{item_name}' stock is now empty.") # Optional: Remove item from dict

if name == "main": while True: print("\n--- Inventory Management ---") print("1. Add Item") print("2. View Inventory")
print("3. Remove Item") print("4. Exit")

choice = input("Enter your choice: ")

if choice == '1':
item = input("Enter item name: ")
try:
qty = int(input("Enter quantity to add: "))
add_item(item, qty)
except ValueError:
print("Invalid quantity. Please enter a number.")
elif choice == '2':
view_inventory()
elif choice == '3':
item = input("Enter item name to remove: ")
try:
qty = int(input("Enter quantity to remove: "))
remove_item(item, qty)
except ValueError:
print("Invalid quantity. Please enter a number.")
elif choice == '4':
print("Exiting inventory management system. Goodbye!")
break
else:
print("Invalid choice. Please try again.")

You might also like