Good Morning Guys!!
Please help me with my doubt
Question: prepare a Dictionary in python about the product and its price (Both the values entered by the user). Write a program to get the Required dictionary.
I have tried the same spyder 3.7 version.
I have written the following code:
n=int(input("How many products will you like to add = "))
for i in range (1,n+1):
Product=str(input("Enter product name= "))
Price=int(input("Enter price= "))
dict2={Product : Price} * n
print(dict2)
Please tell me where am I wrong...
Answers
Answered by
2
First of all, you haven't declared the dictionary outside the for loop so that it can be printed after the loop terminates.
Secondly, why are you even multiplying a scalar to a dictionary?
dict2 = {Product : Price} * n
Corrected Program:
total_items = int(input("Enter total number of items - "))
item_dict = { }
for i in range(0, total_items):
product_name = input("Enter product name - ")
price = float(input("Enter product price - "))
item_dict[product_name] = price
print(item_dict)
Attachments:
gurpritjai:
Thanks a lot!!! :)
Similar questions