0

I have created a class say for eg. fruit. And I have made preset classes for example.

Apple = Fruit("Apple","red","small")

so where I can do Apple.color so that it returns red.

How can I ask the user for a fruit and it would be linked to the class?

my_fruit = input("Choose a fruit.")
my_fruit = Apple #but only as a string

How can I make my_fruit.color return red, similar to the created class of Apple. Would I need to convert this as a strong to a class?

D.M
  • 510
  • 6
  • 14

2 Answers2

3

In Python 2, the following would work as I believe you intend:

class Fruit(object):

    def __init__(self, name, color, size):
        self.name = name
        self.color = color
        self.size = size


Apple = Fruit('Apple', 'red', 'small')

fruit_class = input('Choose type of fruit: ')
print(fruit_class.color)

However, the function input in Python 3 behaves like raw_input does in Python 2. The is no function in Python 3 like Python 2's input. However, effectively the same functionality can be achieved using eval(input()) in Python 3 as described in this post.

The code would look like this:

Apple = Fruit('Apple', 'red', 'small')

fruit_class = eval(input('Choose type of fruit: '))
print(fruit_class.color)

However, use of eval is dangerous as the user can execute arbitrary code against the system. A mapping between the input string values you expect and the user to input and the class instances to which they should map could serve the same use case. For example:

Apple = Fruit('Apple', 'red', 'small')

fruits = {
    'Apple': Apple,
}

fruit_class_type = input('Choose type of fruit: ')
print(fruits[fruit_class_type].color)
Daniel Corin
  • 1,987
  • 2
  • 15
  • 27
  • the first example cannot work, but the last is better. you should even use `fruits.get(fruit_class_type, 'no fruit like this')` to set a default value – PRMoureu Aug 19 '17 at 06:06
  • 1
    The first example _does_ work in Python 2. The second is a reproduction of the first in Python 3. And yes, agreed, there are many ways to improve upon input gathering and fall-through cases in the last example – Daniel Corin Aug 19 '17 at 06:09
  • I will run some tests to make sure that this allows me to pull the correct information from the class but this passed the initial tests. Thanks for the help, Daniel! – TheBrownWallet Aug 19 '17 at 07:06
1

Creating an istance of a class through input

I wish this code is what you intended. I understood that you wanted to create an istance of a class Fruit with attributes for color etc. with the input function. So I think this could be a possible solution.

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


fruit1 = Fruit(
    input("Choose a type of fruit: "),
    input("Choose the color: "),
    input("Choose the size: "))

print("This is your fruit:")
print(fruit1.type, fruit1.color, fruit1.size)

OUTPUT

Choose a type of Fruit: Ananas
Choose the color: orange
Choose the size: big
This is your fruit:
Ananas orange big
PythonProgrammi
  • 22,305
  • 3
  • 41
  • 34