Posts: 42
Threads: 22
Joined: Oct 2021
How can I write the code for the 'predator' name entered from the keyboard to be something like this (without the name appearing in the output, only the ones below):
id -> 1
producer - acer
category -> laptop
price -> 1000
stock -> 5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
products = [ 'id' , 'name' , 'producer' , 'category' , 'price' , 'stock' ]
list1 = [
{
"id" : "1" ,
"name" : "predator" ,
"producer" : "acer" ,
"category" : "laptop" ,
"price" : 1000 ,
"stock" : 5
},
{
"id" : "2" ,
"name" : "monitor1" ,
"producer" : "dell" ,
"category" : "monitor" ,
"price" : 500 ,
"stock" : 10
}
]
product_name = input ( 'Enter the name of product: ' )
for el in val:
if el[ 'name' ] = = product_name:
del el[ 'name' ]
print (el)
|
Posts: 379
Threads: 2
Joined: Jan 2021
Is this what you're looking for?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
products = [ 'id' , 'name' , 'producer' , 'category' , 'price' , 'stock' ]
list1 = [
{
"id" : "1" ,
"name" : "predator" ,
"producer" : "acer" ,
"category" : "laptop" ,
"price" : 1000 ,
"stock" : 5
},
{
"id" : "2" ,
"name" : "monitor1" ,
"producer" : "dell" ,
"category" : "monitor" ,
"price" : 500 ,
"stock" : 10
}
]
product_name = input ( 'Enter the name of product: ' )
for el in list1:
if el [ 'name' ] = = product_name :
for key, value in el.items () :
if key ! = 'name' :
print (key, value)
|
Posts: 42
Threads: 22
Joined: Oct 2021
(Dec-24-2021, 10:01 PM)BashBedlam Wrote: Is this what you're looking for?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
products = [ 'id' , 'name' , 'producer' , 'category' , 'price' , 'stock' ]
list1 = [
{
"id" : "1" ,
"name" : "predator" ,
"producer" : "acer" ,
"category" : "laptop" ,
"price" : 1000 ,
"stock" : 5
},
{
"id" : "2" ,
"name" : "monitor1" ,
"producer" : "dell" ,
"category" : "monitor" ,
"price" : 500 ,
"stock" : 10
}
]
product_name = input ( 'Enter the name of product: ' )
for el in list1:
if el [ 'name' ] = = product_name :
for key, value in el.items () :
if key ! = 'name' :
print (key, value)
|
yes, it's perfect
thanks!
Posts: 1,145
Threads: 114
Joined: Sep 2019
Another way
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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():
for key, value in mydict.items():
if key = = product_name:
for item, description in value.items():
print ( f '{item}: {description}' )
else :
print ( 'No product with that name' )
|
Output: Enter name of product: monitor
id: 2
name: monitor1
producer: dell
category: monitor
price: 500
stock: 10
Posts: 1,838
Threads: 2
Joined: Apr 2017
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 .
Posts: 1,145
Threads: 114
Joined: Sep 2019
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, so mydict[product_name] gets you the value associated with key product_name .
You are right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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
|