I'm working on a python assingment involving creating and using a class with several methods. I won't post the whole thing as it'll be simpler to point out my issue if I instead show an example of what I'm trying to do.
class Fruit(self,color,name):
def __init__(self, color, name):
self.color = color
self.name = name
def return_list(self, index):
print fruit_list[index]
fruit_list = []
fruit_1 = Fruit("red", "apple")
fruit_list.append(fruit_1.color)
fruit_list.append(fruit_1.name)
So the above code works. My issue is getting return_list to work when used outside of the class, as so:
fruit_choice = int(raw_input("What fruit would you like?"))
Fruit.return_list(fruit_choice)
Basically I'm trying to create a method that when called, outputs the item at an index within a list, the index being specified by user input (i.e fruit_choice = 0, printed item is first element of list.)
I have a basic understanding of classes, but getting a somewhat vague method to work like return_list is a little unintuitive. The error message I get:
Fruit.return_list(fruit_choice)
TypeError: unbound method return_list() must be called with Fruit instance as first argument (got int instance instead)
I know that if I got the code to work the output would be "red" if input was 0 instead of "red,"apple", but that's an issue for another time.
If someone could point out to me what I'm doing wrong in creating/calling the return_list method, please and thanks.