Home > other >  Can I take string from a user, and if its definition is in a dict, print the definition in python?
Can I take string from a user, and if its definition is in a dict, print the definition in python?

Time:06-12

I'm relatively new to python, and I'm struggling to implement a program that prompts the user for the name of a fruit (e.g. Apple) and then outputs that fruit's calories in one portion of that fruit (e.g. 130). I also want to ignore any input that isn’t a fruit. [Click here for desired output image][1]

I've tried to solve this problem using a dictionary, to match a name to its corresponding format:

    fruit_dict = {
            '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':'Honeydrew 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'

}
item = input('Item: ').strip().title()
if item in fruit_dict:
    print(fruit_dict.get)

Problem is, I don't know how to take a user output (e.g Apple) and print its definition (e.g 130)

Appreciate you reading this long description. Anyone have ideas to implement such a program?

CodePudding user response:

Dictionary is the correct approach but your implementation is incorrect, I have attached the correct code below, hope that helps

fruit_dict = {
    "Apple": "Calories: 130",
    "Avocado": "Calories: 50",
    "Banana": "Calories: 110",
    "Cantaloupe": "Calories: 50",
    "Grapefruit": "Calories: 60",
    "Grapes": "Calories: 90",
    "Honeydrew Melon": "Calories: 50",
    "Kiwifruit": "Calories: 90",
    "Lemon": "Calories: 15",
    "Lime": "Calories: 20",
    "Nectarine": "Calories: 60",
    "Orange": "Calories: 80",
    "Peach": "Calories: 60",
    "Pear": "Calories: 100",
    "Pineapple": "Calories: 50",
    "Plums": "Calories: 70",
    "Strawberries": "Calories: 50",
    "Sweet Cherries": "Calories: 100",
    "Tangerine": "Calories: 50",
    "Watermelon": "Calories: 80",
}
item = input("Item: ")
print(fruit_dict.setdefault(item, "Not a fruit"))

CodePudding user response:

Keys in dict are unique. So if you declare the dict as

fruit_dict = {
    'Fruit':'Apple', 'Calories':'130',
    'Fruit':'Avocado', 'Calories':'50',
    'Fruit':'Sweet Cherries', 'Calories':'100',
    'Fruit':'Tangerine', 'Calories':'50',
    'Fruit':'Watermelon', 'Calories':'80'
}

the actual fruit_dict will be {'Fruit': 'Watermelon', 'Calories': '80'}

You can use the fruit name as the key, and the calories as the value.

The dictionary can be declared as

fruit_calories_dict = {
    "Apple": 130,
    "Avocado": 50,
    "Banana": 110,
    "Cantaloupe": 50,
    "Grapefruit": 60,
    "Grapes": 90,
    "Honeydrew 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,
}

CodePudding user response:

lower casing all dictionary keys to simplify logic

fruit_dict = {
    "apple": "calories: 130",
    "avocado": "calories: 50",
    "banana": "calories: 110",
    "cantaloupe": "calories: 50",
    "grapefruit": "calories: 60",
    "grapes": "calories: 90",
    "honeydrew melon": "calories: 50",
    "kiwifruit": "calories: 90",
    "lemon": "calories: 15",
    "lime": "calories: 20",
    "nectarine": "calories: 60",
    "orange": "calories: 80",
    "peach": "calories: 60",
    "pear": "calories: 100",
    "pineapple": "calories: 50",
    "plums": "calories: 70",
    "strawberries": "calories: 50",
    "sweet cherries": "calories: 100",
    "tangerine": "calories: 50",
    "watermelon": "calories: 80",
}
fruit=input('Enter fruit name: ')
if fruit.lower().strip() in fruit_dict.keys():
    print(fruit_dict[fruit])

Output:

Enter fruit name: banana
calories: 110

hope this helps...

CodePudding user response:

For a beginner, your idea of using a dictionary is a good sign of your understanding of the problem. However, the implementation is incorrect because you don't have a good grasp of what a python dictionary is.

You want to, given a string, output a value related to that string, am I right?

In a python dictionary, you can map non unique values with a unique key, like so:

myDict = { key: value }

which makes the value accessible by calling myDict[key]. In your case, the keys are not unique (Fruit & Calories).

So, your keys would be the names of your fruits, and the values associated with them are the calories.

fruit_dict = {
    "Apple": 130,
    "Avocado": 50,
    "Banana": 110,
    ...
}

Assuming the user correctly inputs the capitalized string, the value is now accessible by calling fruit_dict[user_input].

To only take into account the fruits that are included in your dictionary, you need to check if the input is included in the list of keys of the dictionary using list(fruit_dict.keys()).

if user_input in list(fruit_dict.keys()):
  # do something here

Good luck!

  • Related