Looping Algorithms
defining dictionaries:
fruit = {
"bananas": 0.62,
"apples": 1.23,
"grapes": 2.24,
"strawberries": 1.37,
"oranges": 1.45
}
#veg + their prices
vegs = {
"potatoes": 0.78,
"tomatoes": 1.91,
"onions": 0.97,
"carrots": 0.49,
"bell peppers": 1.46
}
#combines fruit and veg into one dictionary
food = dict()
food["fruit"] = fruit
food["vegs"] = vegs
print("fruit and their price:" + str(fruit))
print("vegetables and their price:" + str(vegs))
for loop with index:
overall_price = 0
overall_price2 = 0
#for-loop for summing all values in the 'fruit' dictionary
for i in fruit:
overall_price = overall_price + fruit[i]
#for-loop for summing all values in the 'vegs' dictionary
for i in vegs:
overall_price2 = overall_price2 + vegs[i]
print("overall price of fruits: $", str(overall_price))
print("overall price of vegetables: $", str(overall_price2))
for loop (without index):
cart = []
for x in fruit:
cart.append(x) #adds items from fruit
print("your cart: " + str(cart))
for x in vegs:
cart.append(x) #adds items from vegetables
print("your cart: " + str(cart))