3-1/Python 과제&실습

12주차-실습&과제

Donghun Kang 2024. 5. 31. 19:55

 

class Robot:
    def __init__(self, name, battery_capacity=100, b_alram=50):
        self.name = name
        self.__battery_capacity = battery_capacity
        self.__battery_level = battery_capacity
        self.b_alram = b_alram
        print(f"Hello my name is {self.name}")

    def __battery_check(self):
        if self.__battery_level < self.b_alram:
            print(f"Battery has {self.__battery_level} remaining")

    def clean_room(self):
        self.__battery_level -= 10
        print(f"{self.name} is cleaning the room")
        self.__battery_check()

    def cook_meal(self):
        self.__battery_level -= 20
        print(f"{self.name} is cooking a meal")
        self.__battery_check()

    def charge_battery(self, amount):
        self.__battery_level += amount  
        if self.__battery_level > self.__battery_capacity:
            self.__battery_level = self.__battery_capacity
        print(f"Battery has {self.__battery_level} remaining")

robot1 = Robot('AI-1')
robot1.clean_room()
robot1.clean_room()
robot1.cook_meal()
robot1.cook_meal()
robot1.charge_battery(40)

 

출력값

 

 

class Pet:
    def __init__(self, name):
        self.name = name
        self.happiness = 50
        self.hunger = 50
        self.energy = 50

    def feed(self, amount):
        self.hunger -= amount
        if self.hunger < 0:
            self.hunger = 0
        self.energy += amount 
        if self.energy > 100:
            self.energy = 100
        print(f"{self.name}에게 먹이를 주었습니다. 배고픔: {self.hunger}, 에너지: {self.energy}")

    def play(self, time):
        self.happiness += time
        if self.happiness > 100:
            self.happiness = 100
        self.energy -= time
        if self.energy < 0:
            self.energy = 0
        print(f"{self.name}와 놀았습니다. 행복도: {self.happiness}, 에너지: {self.energy}")

    def sleep(self, time):
        self.energy += time
        if self.energy > 100:
            self.energy = 100
        print(f"{self.name}가 {time}시간 동안 잠을 잤습니다. 에너지: {self.energy}")

    def status(self):
        print(f"{self.name}의 현재 상태 - 행복도: {self.happiness}, 배고픔: {self.hunger}, 에너지: {self.energy}")

class Dog(Pet):
    def bark(self):
        print(f"{self.name}가 멍멍 짖습니다.")

class Cat(Pet):
    def meow(self):
        print(f"{self.name}가 야옹 웁니다.")

def check_status(pet):
    if pet.happiness < 20:
        print(f"경고: {pet.name}의 행복도가 너무 낮습니다!")
    if pet.hunger > 80:
        print(f"경고: {pet.name}의 배고픔이 너무 높습니다!")
    if pet.energy < 20:
        print(f"경고: {pet.name}의 에너지가 너무 낮습니다!")

def main():
    pet = Dog("바둑이")
    
    while True:
        action = input("무슨 행동을 할까요? (feed/play/sleep/status/quit): ")
        if action == "feed":
            amount = int(input("얼마나 먹이를 줄까요? "))
            pet.feed(amount)
        elif action == "play":
            time = int(input("얼마나 놀아줄까요? "))
            pet.play(time)
        elif action == "sleep":
            time = int(input("얼마나 잘까요? "))
            pet.sleep(time)
        elif action == "status":
            pet.status()
        elif action == "quit":
            break
        else:
            print("올바른 명령어를 입력하세요.")
        
        check_status(pet)

if __name__ == "__main__":
    main()

'3-1 > Python 과제&실습' 카테고리의 다른 글

14주차-실습&과제  (0) 2024.06.13
13주차-실습&과제  (0) 2024.06.13
11주차-실습&과제  (0) 2024.05.21
10주차-실습&과제  (0) 2024.05.21
9주차-실습&과제  (0) 2024.05.21