n = int(input('Enter number of products : '))
prod = {}
for i in range(n):
ProductName = input('Enter product name : ')
ProductPrice = int(input('Enter its price : '))
prod[ProductName] = ProductPrice
print(prod) #prod = {'prod1': 100, 'prod2': 200, 'prod3': 300}
ProductName = input('enter price')
print(ProductName)
for ProductName, prod[ProductPrice] in prod:
if prod[ProductName] < ProductName:
print(ProductName)
When I enter 200 in ProductName, I want to print a prod1 that has a value of 100 less than 200, but I get an error. I ask for your help me.
Traceback (most recent call last):
File "C: Grade 2 File/Construction Plant Introduction/#11.Dictionaries_Exercise/#2.py", line 28, in <module>
for ProductName, prod[ProductPrice] in prod:
ValueError: too many values to unpack (expected 2)
dictionary returns a key in for loop.
If you want to use both key and value at the same time, use it as below.
d = {'a': 1, 'b': 2, 'c': 3}
for k in d:
print(k) # 'a', 'b', 'c'
for k, v in d.items():
print((k, v)) # ('a', 1), ('b', 2), ('c', 3)
prod= {'prod1': 100, 'prod2': 200, 'prod3': 300}
_input = 200
for key in prod: #
if prod[key] < _input:
print(key)
In #
, prod
has the dictionary key value prod1, prod2, prod3
so it cannot be unpacked with two.
793 M2 Mac fails to install rbenv install 3.1.3 due to errors
778 GDB gets version error when attempting to debug with the Presense SDK (IDE)
1261 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
871 Uncaught (inpromise) Error on Electron: An object could not be cloned
© 2025 OneMinuteCode. All rights reserved.