mercredi 31 octobre 2018

Python if statement always showing the else statement [duplicate]

This question already has an answer here:

I just started learning python and was doing a dummy project as a test but the result always go to the else statement, hope I can get help! here is the full code.

class PetLover(object):
    def __init__(self, livingPlace, hoursAtHome):
        self.p = livingPlace
        self.h = hoursAtHome

    def getRecommendation(self):
        if self.p == 'h':
            if self.h in range(18, 24):
                return "Pot bellied pig"
            elif self.h in range(10, 17):
                return "Dog"
            elif self.h in range(1, 10):
                return "Snake"
            else:
                return "No recommendation"
        elif self.p == 'a':
            if self.h in range(10, 24):
                return "Cat"
            elif self.h in range(1, 9):
                return "Hamster"
            else:
                return "No recommendation"
        elif self.p == 'd':
            if self.h in range(6, 24):
                return "Fish"
            elif self.h in range(1, 5):
                return "Ant Farm"
            else:
                return "No recommendation"
        else:
            return "No recommendation"

livePlace = input("Do you live in house(H), Apartment(A) or Dorm(D)?")
hoursHome = input("How many hours do you spend at home?")

p = PetLover(livePlace, hoursHome)

print(p.getRecommendation())

Thank you!

Aucun commentaire:

Enregistrer un commentaire