In this problem I want to return a grocery store dictionary with the name as the key and the price the value. The price must be less than my budget and the distance must be less than maxDistance. So the output for this problem should be {'Kroger': 2.79, 'Walgreens': 1.99, 'Sprouts': 3.45} . This is the code that I have, but it returns an IndexError.
def friendsgiving(stores, budget, maxDistance):
if stores == [] and budget == 0 and maxDistance
== 0:
return {}
else:
storeDict = {}
(name, price, distance)
= stores[0]
friendsgiving(stores[1:], budget, maxDistance)
if price < budget and
distance < maxDistance:
return storesDict[name] == price
print(friendsgiving( [('Sprouts', 3.45, 2),('ALDI', 3.69,
6),('Walgreens', 1.99, 1),('Kroger', 2.79, 4)], 3.50, 5))
def friendsgiving(stores, budget, maxDistance):
if stores == [] and budget == 0 and maxDistance == 0:
return {}
else:
storeDict = {}
for i in range(len(stores)):
(name, price, distance) = stores[i]
if price < budget and distance < maxDistance:
storeDict.update({name:price})
return storeDict
print(friendsgiving( [('Sprouts', 3.45, 2),('ALDI', 3.69, 6),('Walgreens', 1.99, 1),('Kroger', 2.79, 4)], 3.50, 5))
I Have made the necessary changes and got the output.Hope it helps you.
Get Answers For Free
Most questions answered within 1 hours.