简体   繁体   中英

Python 3.6 - user input call class variable

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.

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'

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM