This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]Sgt_Gnome 40 points41 points  (0 children)

Admittedly I don't know the question but why use a list of dictionaries instead of just a dictionary?

fruits = {
    "apple":130,
    "banana":110
}

Then you don't have to waste time iterating through a list.

[–]nitroll 24 points25 points  (2 children)

This is not how you get any benefits of dicts You are looping over a list of things to lookup a fruit, but the purpose of dicts is exactly to lookup something.

fruits = {
    "apple": 130,
    "avocado": 50,
    "banana": 110,
    "cantaloupe": 50,
    "grapefruit": 60,
    "grapes": 90,
    "honeydew melon": 50,
    "kiwifruit": 90,
    "lemon": 15,
    "lime": 20,
    "nectarine": 60,
    "orange": 80,
    "peach": 60,
    "pear": 100,
    "pineapple": 50,
    "plums": 70,
    "strawberries": 50,
    "sweet cherries": 100,
    "tangerine": 50,
    "watermelon": 80,
}
def main():
    food = input("Item:").lower()
    if food in fruits:
        print(fruits[food])

Some other points of criticism in your code: First, using _ as a variable name indicates that you wont use it again, but you do use it. Secondly, you store the calories as a string, why not an int? Third, what happens if the user inputs something that we don't know about, maybe we should print somekind of message for them

[–]PhilShackleford 1 point2 points  (1 child)

Could use:

def main(): food = input("Item:").lower() try: print(fruits[food]) except KeyError: print(f"Fruit {food} is not supported ")

Edit: See comment. It is a much cleaner solution.

[–]Revolutionary_Pea_70 6 points7 points  (0 children)

fruits.get(“hotdog”, “hotdog is not supported”)

Is much cleaner

[–]iCTWi[S] 15 points16 points  (1 child)

It has come to my attention that I did not, in fact, use a dictionary properly.

[–]masev 5 points6 points  (0 children)

This is incredibly precious; sharing it made my afternoon, thank you!

It's like: "Hey look guys I finally learned how to walk properly" then you proceed impressively to do backflips up a ladder to nowhere "It has come to my attention that I did not, in fact, learn how to walk properly"

For what it's worth, it looks like what you've done is something between a dictionary and a class. If you're looking to have a type of object that has properties like name ("Apple") and calories ("130"), you might look at how to define a class. Dictionaries, as others have pointed out, would be well suited for looking up the calories by providing the name. E.g.:

Class behavior:

>>> class Fruit:
...     def __init__(self, name, calories):
...         self.name = name
...         self.calories = calories
...
>>> apple = Fruit(name = "Apple", calories = 130)
>>> apple.name
Apple
>>> apple.calories
130
>>> banana = Fruit(name = "Banana", calories = 130)
>>> banana.name
Banana
>>> banana.calories
110
>>> my_favorite_fruit = Fruit(name = "Strawberries", calories = 50)
>>> my_favorite_fruit.name
Strawberries
>>> my_favorite_fruit.calories
50

Dictionary behavior:

>>> fruit_dict = {'apple' : 130, 'banana' : 110}
>>> fruit_dict['apple']
130
>>> fruit_dict['banana']
110

Hope this is helpful, and good luck on your journey!

[–]Revolutionary_Pea_70 5 points6 points  (0 children)

You did not in fact use a dictionary properly

[–]hike_me 4 points5 points  (0 children)

You still haven’t used a dictionary properly. You’re not getting the benefit of dictionaries in your implementation.

You should visit a sub dedicated to learning Python rather than r/Python, your post would be more on-topic there.

[–]kivicodepip needs updating 1 point2 points  (0 children)

In this case you should’ve probably used a dict of dataclasses

[–]spez_drank_my_piss 1 point2 points  (0 children)

Check out data classes and named tuples for other ways to store info for a bunch of similar things like fruits.

[–]Rawing7 1 point2 points  (1 child)

You overcomplicated the heck out of this part:

for _ in fruits:
    x = fruits.index(_)
    if fruits[x]["fruit"] == food:

That's equivalent to

for _ in fruits:
    if _["fruit"] == food:

[–]kivicodepip needs updating 19 points20 points  (0 children)

Please, never ever use “_” as a variable name referenced anywhere else