1:19 Trying to add "orange" to go with lemon. Text problem


#1

class Fruit(object):
    """A class that makes various tasty fruits."""
    def __init__(self, name, color, flavor, poisonous):
        self.name = name
        self.color = color
        self.flavor = flavor
        self.poisonous = poisonous

    def description(self):
        if self.color[0] == "a" or "e" or "i" or "o" or "u":
            print "I'm an %s %s and I taste %s." % (self.color, self.name, self.flavor)
        else:
            print "I'm a %s %s and I taste %s." % (self.color, self.name, self.flavor)
            
    def is_edible(self):
        if not self.poisonous:
            print "Yep! I'm edible."
        else:
            print "Don't eat me! I am super poisonous."

lemon = Fruit("lemon", "yellow", "sour", False)
canteloupe = Fruit("canteloupe", "beige", "bland", True)
orange = Fruit("orange", "orange", "sweet", False)

lemon.description()
lemon.is_edible()
canteloupe.description()
canteloupe.is_edible()
orange.description()
orange.is_edible()

Here is my code. Basically I am just trying to put "I am a yellow..." or "I am an orange..." for things that start with vowels. But no matter what I do both always print "I am an yellow/orange..."

Can someone help me out here? I tried - str(if self.color[0[)== etc but I cant get this to work.


#2

print False or "e"

"e"

print bool("e")

True

if "e":
    print '"e" is truthy'
else:
    print '"e" is falsy'

"e" is truthy

#3

I'm sorry, I dont understand this at all. Could you elaborate please?


#4

Why are you using self.color for your if statement to test if the first letter of the fruit is a vowel?


#5

In the example the color is listed first. "I am a(n) "color" "fruit"...." so it would be "I am and orange orange..." and what not.


#6

Oops, sorry 'bout that. Hmmm... print self.color[0] does print the first letter of the color but doesn't like the comparison for some reason. I also tried <> and switched the 'an' and 'a' words of the output but still had the same issue. Need to think on this some more.


#7

Alright... after you do your first comparison

if self.color[0] == "a"

then you state

or "e" or "i" or "o" or "u":

so if you break it down with parentheses you would have

if (self.color[0] == "a") or ("e") or ("i") or ("o") or ("u"):

which would explain why it doesn't do the compare for the other 'vowels'. Is that enough of a hint?


#8

No, it doesn't. I am sorry. I am not very smart.

EDIT: I suppose I could use a bunch of elif statements like
elif self.color == "a":
print "X"
elif self.color == "e"
print "X'
elif self.color == "i"
print "X"

So on but I feel like there is an easier way. I don't see what I am missing.


#9

You are on the right track. One way to still use 'or' is make it all one statement.

    if self.color[0] == "a" or \
       self.color[0] == "e" or \
       self.color[0] == "i" or \
       self.color[0] == "o" or \
       self.color[0] == "u":

A cleaner way is to use 'in' to put it all on one line. This will test membership in a list
if self.color[0] in ["a", "e", "i", "o", "u"]: