mirror of
https://github.com/DavidGailleton/42-Piscine_Python.git
synced 2026-03-14 05:06:55 +01:00
03/ex5 + flake8
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
from logging import raiseExceptions
|
||||
from queue import Empty
|
||||
import sys
|
||||
|
||||
|
||||
@@ -7,33 +5,39 @@ def current_inventory(inventory: dict[str, int], total_items: int) -> None:
|
||||
print("\n=== Current Inventory ===")
|
||||
for item in inventory:
|
||||
percent_of_inventory = inventory[item] / total_items * 100
|
||||
if (inventory[item] > 1):
|
||||
print(f"{item}: {inventory[item]} units ({percent_of_inventory:.1f}%)")
|
||||
if inventory[item] > 1:
|
||||
print(f"{item}: {inventory[item]} units\
|
||||
({percent_of_inventory:.1f}%)")
|
||||
else:
|
||||
print(f"{item}: {inventory[item]} unit ({percent_of_inventory:.1f}%)")
|
||||
print(f"{item}: {inventory[item]} unit\
|
||||
({percent_of_inventory:.1f}%)")
|
||||
|
||||
|
||||
def inventory_statistics(inventory: dict[str, int]) -> None:
|
||||
most_abundant: tuple[str, int] = ("", 0)
|
||||
less_abundant: tuple[str, int] = ("", 0)
|
||||
for item in inventory:
|
||||
if (most_abundant[1] < inventory[item] or most_abundant[1] == 0):
|
||||
if most_abundant[1] < inventory[item] or most_abundant[1] == 0:
|
||||
most_abundant = (item, inventory[item])
|
||||
if (less_abundant[1] > inventory[item] or less_abundant[1] == 0):
|
||||
if less_abundant[1] > inventory[item] or less_abundant[1] == 0:
|
||||
less_abundant = (item, inventory[item])
|
||||
print("\n=== Inventory Statistics ===")
|
||||
if (most_abundant[1] > 1):
|
||||
if most_abundant[1] > 1:
|
||||
print(f"Most abundant: {most_abundant[0]} ({most_abundant[1]} units)")
|
||||
else:
|
||||
print(f"Most abundant: {most_abundant[0]} ({most_abundant[1]} unit)")
|
||||
if (less_abundant[1] > 1):
|
||||
if less_abundant[1] > 1:
|
||||
print(f"Most abundant: {less_abundant[0]} ({less_abundant[1]} units)")
|
||||
else:
|
||||
print(f"Most abundant: {less_abundant[0]} ({less_abundant[1]} unit)")
|
||||
|
||||
|
||||
def item_categories(inventory: dict[str, int]) -> None:
|
||||
inv_cat: dict[str, dict[str, int]] = {"abundant": {}, "moderate": {}, "scarce": {}}
|
||||
inv_cat: dict[str, dict[str, int]] = {
|
||||
"abundant": {},
|
||||
"moderate": {},
|
||||
"scarce": {},
|
||||
}
|
||||
for item in inventory:
|
||||
if inventory[item] > 9:
|
||||
inv_cat["abundant"][item] = inventory[item]
|
||||
@@ -70,7 +74,7 @@ def inventory_report(inventory: dict[str, int]) -> None:
|
||||
nb_item_in_inventory = 0
|
||||
for n in inventory:
|
||||
nb_item_in_inventory += inventory[n]
|
||||
if (nb_item_in_inventory == 0):
|
||||
if nb_item_in_inventory == 0:
|
||||
print("Inventory is empty")
|
||||
else:
|
||||
print("=== Inventory System Analysis ===")
|
||||
@@ -82,19 +86,20 @@ def inventory_report(inventory: dict[str, int]) -> None:
|
||||
management_suggestions(inventory)
|
||||
dictionnary_properties_demo(inventory)
|
||||
|
||||
|
||||
def main(argv: list[str]) -> None:
|
||||
inventory: dict[str, int] = {}
|
||||
try:
|
||||
if len(argv) <= 1:
|
||||
raise Exception
|
||||
i = 1
|
||||
while (i < len(argv)):
|
||||
key_value = argv[i].split(':')
|
||||
if (len(key_value) != 2):
|
||||
while i < len(argv):
|
||||
key_value = argv[i].split(":")
|
||||
if len(key_value) != 2:
|
||||
raise Exception("Invalid input")
|
||||
if (int(key_value[1]) > 0):
|
||||
if int(key_value[1]) > 0:
|
||||
inventory[key_value[0]] = int(key_value[1])
|
||||
if (int(key_value[1]) < 0):
|
||||
if int(key_value[1]) < 0:
|
||||
raise Exception("Nb of item in inventory cannot be negative")
|
||||
i += 1
|
||||
except Exception as err:
|
||||
|
||||
Reference in New Issue
Block a user