0

I'm very new to Python (and programming in general) so I apologize if I am asking the wrong question.

I want to create a tool for looking up data from a dictionary where the user inputs a string, and if the string matches a variable in the dictionary, the attributes of the variable are printed. I am having trouble finding a way to convert the string input into a pre-defined variable. Here is a summary of what I have so far:

class Fruit:
  def __init__(self, name, color):
    self.name = name
    self.color = color

banana = Fruit('banana', 'yellow')

fruit_choice = input("What fruit would you like to know about?")

From here, I have tried a variety of ways to have the input string ("banana") call the variable(banana) and then perform other methods defined under the class. Using a dictionary key doesn't work because I want to include multiple attributes rather than just 1.

2
  • 2
    "looking up data from a dictionary" Well, where is said dictionary? "Using a dictionary key doesn't work because I want to include multiple attributes" You only want to store a single Fruit instance, no? Commented Apr 27, 2018 at 15:26
  • I did not include a dictionary in my example for the sake of keeping it simple Commented Apr 27, 2018 at 16:28

4 Answers 4

1

If you use a dictionary where the key is the name of the fruit, and the value is your Fruit instance, you could simply look up the values, and override __str__ with whatever you want the fruit description to be:

class Fruit:
  def __init__(self, name, color):
    self.name = name
    self.color = color

  def __str__(self):
    return '{}s are {}'.format(self.name, self.color)

dct = {}
dct['banana'] = Fruit('banana', 'yellow')

Now you can use your current method to find a fruit's attributes:

In [20]: ask = input('What fruit would you like to know about? ')
What fruit would you like to know about? banana

In [21]: dct.get(ask, 'Fruit not found')
Out[21]: bananas are yellow

This will also handle cases where a fruit is not in your dictionary:

In [23]: dct.get('apple', 'Fruit not found')
Out[23]: 'Fruit not found'
Sign up to request clarification or add additional context in comments.

Comments

0

Here's mine:

class Fruit:
  def __init__(self, name, color, price):
    self.name = name
    self.color = color
    self.price = price
  def __str__(self):
    return "name: "+self.name+" color: "+self.color+" price: $"+str(self.price)

dict_of_fruits = {}
banana = Fruit('banana', 'yellow', 3)
apple = Fruit('apple', 'red', 2)

dict_of_fruits[banana.name] = banana
dict_of_fruits[apple.name] = apple


fruit_choice = input("What fruit would you like to know about?")

if fruit_choice in dict_of_fruits:
  print("Here are the attr. of ",fruit_choice,': ',dict_of_fruits[fruit_choice])
else:
  print("Sorry I could not find ",fruit_choice," in my records")

I included a __str__() method to make the print a little nicer, as well as a new price property since you mentioned there were more than 2 attr's

Output:

What fruit would you like to know about? banana
Here are the attr. of  banana :  name: banana color: yellow price: $3

1 Comment

Thank you! This is very helpful. Would the variable fruit_choice then be equivalent to the banana variable, in such a way that I could use other Fruit defined methods using fruit_choice.method() ?
0

You should still use a lookup dictionary. Its values may be another dict that holds each fruit's attributes, or a Fruit object.

Comments

0

you can use something like that, this only a draft to show the approach

class Fruit:
  def __init__(self, name, color):
    self.name = name
    self.color = color

  def __str__(self):
    return "{} : {}".format(self.name, self.color)

fruit_dict = dict()
banana = Fruit('banana', 'yellow')
fruit_dict.update({banana.name : banana})

fruit_choice = input("What fruit would you like to know about?")
user_fruit = fruit_dict.get(fruit_choice)
if user_fruit is not None:
    print(user_fruit)

output (if input was banana)

banana : yellow

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.