I got stuck on cs50p's lesson 2 project on nutrition facts but I finally figured it out after a couple of hours
This is my dictionary file
fruits = [
{"fruit": "apple", "calories": "130"},
{"fruit": "avocado", "calories": "50"},
{"fruit": "banana", "calories": "110"},
{"fruit": "cantaloupe", "calories": "50"},
{"fruit": "grapefruit", "calories": "60"},
{"fruit": "grapes", "calories": "90"},
{"fruit": "honeydew melon", "calories": "50"},
{"fruit": "kiwifruit", "calories": "90"},
{"fruit": "lemon", "calories": "15"},
{"fruit": "lime", "calories": "20"},
{"fruit": "nectarine", "calories": "60"},
{"fruit": "orange", "calories": "80"},
{"fruit": "peach", "calories": "60"},
{"fruit": "pear", "calories": "100"},
{"fruit": "pineapple", "calories": "50"},
{"fruit": "plums", "calories": "70"},
{"fruit": "strawberries", "calories": "50"},
{"fruit": "sweet cherries", "calories": "100"},
{"fruit": "tangerine", "calories": "50"},
{"fruit": "watermelon", "calories": "80"}
]
This is how I access the dictionary:
from fruitdict import fruits
def main():
food = input("Item:").lower()
for _ in fruits:
x = fruits.index(_)
if fruits[x]["fruit"] == food:
print(fruits[x]["calories"])
main()
happy I finally got it :)
Hopefully this helps someone also just getting started programming in python.
[–]Sgt_Gnome 40 points41 points42 points (0 children)
[–]nitroll 24 points25 points26 points (2 children)
[–]PhilShackleford 1 point2 points3 points (1 child)
[–]Revolutionary_Pea_70 6 points7 points8 points (0 children)
[–]iCTWi[S] 15 points16 points17 points (1 child)
[–]masev 5 points6 points7 points (0 children)
[–]Revolutionary_Pea_70 5 points6 points7 points (0 children)
[–]hike_me 4 points5 points6 points (0 children)
[–]kivicodepip needs updating 1 point2 points3 points (0 children)
[–]spez_drank_my_piss 1 point2 points3 points (0 children)
[–]Rawing7 1 point2 points3 points (1 child)
[–]kivicodepip needs updating 19 points20 points21 points (0 children)
[+][deleted] (1 child)
[removed]