Python Forum
how can I display only desired items?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how can I display only desired items?
#6
(Dec-25-2021, 03:38 PM)ndc85430 Wrote: Why the unnecessary loop on line 25? Dicts let you access items by key, so mydict[product_name] gets you the value associated with key product_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


Reply


Messages In This Thread
how can I display only desired items? - by 3lnyn0 - Dec-24-2021, 09:32 PM
RE: how can I display only desired items? - by menator01 - Dec-25-2021, 06:49 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display <IPython.core.display.HTML object>? pythopen 3 51,332 May-06-2023, 08:14 AM
Last Post: pramod08728
  Json filter is not capturing desired key/element mrapple2020 1 2,121 Nov-24-2022, 09:22 AM
Last Post: ibreeden
  ERROR: importing desired module mbgamer28 0 2,180 Apr-05-2021, 07:46 PM
Last Post: mbgamer28
  Not rounding to desired decimal places? pprod 2 3,435 Mar-05-2021, 11:11 AM
Last Post: pprod
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 3,222 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Why this code not getting desired output ? MDRI 2 3,541 Sep-18-2020, 02:11 AM
Last Post: MDRI
  showing only desired part of a plot grknkilicaslan 1 3,880 Jul-10-2020, 03:51 PM
Last Post: Marbelous
  [Beginner] Code is not producing desired result fakej171 2 3,454 Mar-21-2020, 10:26 AM
Last Post: buran
  Not Getting the Desired value using json.dumps saurabh210 0 2,098 Feb-03-2020, 06:48 PM
Last Post: saurabh210
  Code isn't working in several IDE's when other people are getting desired output. SuperSymmetry 1 3,170 Jul-22-2017, 07:03 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020