Dec-25-2021, 06:49 PM
(This post was last modified: Dec-25-2021, 08:35 PM by menator01.
Edit Reason: updated code
)
(Dec-25-2021, 03:38 PM)ndc85430 Wrote: Why the unnecessary loop on line 25? Dicts let you access items by key, somydict[product_name]
gets you the value associated with keyproduct_name
.
You are right.
#! /usr/bin/env python3 mydict = { 'predator': { 'id': 1, 'name': 'predator', 'producer': 'acer', 'category': 'laptop', 'price': 1000, 'stock': 5 }, 'monitor': { 'id': 2, 'name': 'monitor1', 'producer': 'dell', 'category': 'monitor', 'price': 500, 'stock': 10 } } product_name = input('Enter name of product: ').lower() if product_name in mydict.keys(): print() text = f'Details for {product_name.title()}' widths = [] for n in mydict[product_name].keys(): widths.append(len(n)) width = max(widths) print(text) print(f'-' * len(text)) print() for item, desc in mydict[product_name].items(): if len(item) < width: space = (width - len(item)+1) else: space = 1 print(f'{item.upper()}: {" " * space}{desc}') else: print('No product with that name')
Output:Enter name of product: predator
Details for Predator
--------------------
ID: 1
NAME: predator
PRODUCER: acer
CATEGORY: laptop
PRICE: 1000
STOCK: 5
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts