Python if语句始终显示else语句

时间:2018-10-31 10:03:46

标签: python if-statement

我刚刚开始学习python并正在做一个虚拟项目作为测试,但结果总是转到else语句,希望我能得到帮助!  这是完整的代码。

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())

谢谢!

0 个答案:

没有答案