Untitled

 avatar
unknown
python
2 years ago
2.0 kB
4
Indexable
class Man:
    __isSleeping = False

    def __init__(self, name, age, endurance):
        self.__name = name
        self.__age = age
        if endurance > 100:
            self._endurance = 100
        elif endurance < 0:
            self._endurance = 0
        else:
            self._endurance = endurance
        self._currentEndurance = self._endurance

    def Sleep(self):
        self._printStats()
        if self._currentEndurance < self._endurance * 0.2:
            print(f"{self.getName()} is sleeping...")
            self.__isSleeping = True
        else:
            print(f"{self.getName()} doesnt want to sleep!")

    def _printStats(self):
        print(f"{self.getName()} has {round(self._currentEndurance)}"
              f"/{round(self._endurance)} endurance "
              f"which is "
              f"{round(self._currentEndurance/self._endurance * 100)}%.")

    def getName(self):
        return self.__name

    def getIsSlepping(self):
        return self.__isSleeping

class Student(Man):
    __course = 1
    __lessonsLearned = 0

    def setCourse(self, course):
        self.__course = course
        self._endurance = self._endurance * (1 - self.__course * 0.1)
        self._currentEndurance = self._currentEndurance * (1 - self.__course * 0.1)
        print(f"{self.getName()} is on {self.__course} course.")
        self._printStats()

    def learnLesson(self):
        if not self.getIsSlepping():
            self._currentEndurance -= 15 * (1 + self.__lessonsLearned * 0.25)
            self.__lessonsLearned += 1
        print(f"{self.getName()} learned the {self.__lessonsLearned} lesson.")
        self._printStats()
        self.Sleep()
        return self.getIsSlepping()

    def getLessonsLearned(self):
        return self.__lessonsLearned

peter = Man("Peter", 23, 85)
peter.Sleep()

print()

jack = Student("Jack", 21, 85)
jack.Sleep()
jack.setCourse(2)
print()
while (not jack.learnLesson()):
    print()
print(f"\nStudent {jack.getName()}"
      f"learned {jack.getLessonsLearned()} lessons.")
Editor is loading...