Python Consider a dictionary, items_for_sale t h at represents items and their p
ID: 3739324 • Letter: P
Question
Python
Consider a dictionary, items_for_sale that represents items and their prices. The dictionary keys are items' names and the values are their prices.
A customer wants to purchase a gift item and would like to pay more than $10 but no more than $20. We would like to create a set that contains only the items in the requested price range. We'll call this new set giftable_items.
Write a set comprehension for this new dictionary.
Example:
giftable_items will be: {'dress', 'scarf', 'pants', 'umbrella', 'belt'}
Note that the order is not important here.
Please note that to get any credit on this question your answer must be in the form of a set comprehension.
Start with:
giftable_items = {
Explanation / Answer
Screenshot
--------------------------------------------------------------------------------------------------------------------
Code:-
#items_for_sale = {'shirt': 8, 'pants': 20, 'shorts': 10, 'dress': 12,
#'hat': 22, 'belt': 15, 'scarf': 19, 'umbrella': 11,
#'coat': 50, 'shoes': 30, 'lotion': 5, 'candle': 7}
#variable for dictionary
items_for_sale={}
#variable for set
giftable_items=set()
#variable for while looping
addGift='yes'
#loop for entering data into dictionary
while addGift=='yes':
#user prompt to enter item name
k=input("Enter gift item name :")
#user prompt to enter item price
v=input("Enter price of the gift item :")
items_for_sale[k]=int(v)
#yes/no question for next data enter
addGift = input('Would you like to add another Gift? (yes or no): ')
#end of while loop
if addGift == 'no':
break
#for loop to get set values
#get item name as key and price as value
for key,value in items_for_sale.items():
#compare price greater than 10 and less than equal20
if value>10 and value<=20:
#add item name in set
giftable_items.add(key)
#print set
print ('giftable_items will be:',giftable_items)
------------------------------------------------------------------------------------------------------
Note:-
I am using python 3.6
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.