Created at : 2024-09-02 21:50
Auther: Soo.Y

11. ν”„λ‘œμ νŠΈ (Projects)

이 μž₯μ—μ„œλŠ” μ§€κΈˆκΉŒμ§€ 배운 파이썬의 κΈ°μ΄ˆλΆ€ν„° κ³ κΈ‰ μ£Όμ œκΉŒμ§€μ˜ λ‚΄μš©μ„ μ’…ν•©ν•˜μ—¬, μ‹€μ œ ν”„λ‘œμ νŠΈλ₯Ό 톡해 μ‹€μŠ΅ν•΄λ³΄λŠ” μ‹œκ°„μ„ κ°€μ§€κ² μŠ΅λ‹ˆλ‹€. κ°„λ‹¨ν•œ ν”„λ‘œμ νŠΈλ₯Ό μˆ˜ν–‰ν•˜λ©΄μ„œ 파이썬의 λ‹€μ–‘ν•œ κΈ°λŠ₯κ³Ό κ°œλ…λ“€μ„ μ‹€μ œλ‘œ μ–΄λ–»κ²Œ μ μš©ν•˜λŠ”μ§€ ν•™μŠ΅ν•˜κ²Œ λ©λ‹ˆλ‹€. μ•„λž˜μ—μ„œλŠ” μ„Έ κ°€μ§€ μ˜ˆμ‹œ ν”„λ‘œμ νŠΈλ₯Ό μ œμ‹œν•©λ‹ˆλ‹€: κ°„λ‹¨ν•œ 계산기, ν…μŠ€νŠΈ 기반 κ²Œμž„, 파일 관리 μ‹œμŠ€ν…œ. 이 ν”„λ‘œμ νŠΈλ“€μ„ 톡해 파이썬의 μ‹€μš©μ μΈ ν™œμš© 방법을 읡힐 수 μžˆμŠ΅λ‹ˆλ‹€.

11.1 κ°„λ‹¨ν•œ 계산기

κ°„λ‹¨ν•œ 계산기 ν”„λ‘œμ νŠΈλŠ” 파이썬의 기본적인 μ—°μ‚° κΈ°λŠ₯을 μ΄μš©ν•˜μ—¬ μ‚¬μš©μžλ‘œλΆ€ν„° μž…λ ₯을 λ°›μ•„ λ§μ…ˆ, λΊ„μ…ˆ, κ³±μ…ˆ, λ‚˜λˆ—μ…ˆμ„ μˆ˜ν–‰ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ λ§Œλ“œλŠ” κ²ƒμž…λ‹ˆλ‹€. 이 ν”„λ‘œμ νŠΈλŠ” 파이썬의 기초 문법과 ν•¨μˆ˜, 쑰건문, λ°˜λ³΅λ¬Έμ„ μ‹€μŠ΅ν•˜λŠ” 데 도움이 λ©λ‹ˆλ‹€.

11.1.1 μš”κ΅¬ 사항

  • μ‚¬μš©μžλŠ” 두 μˆ«μžμ™€ μ—°μ‚°μžλ₯Ό μž…λ ₯ν•©λ‹ˆλ‹€.
  • ν”„λ‘œκ·Έλž¨μ€ μž…λ ₯된 μ—°μ‚°μžμ— 따라 λ§μ…ˆ, λΊ„μ…ˆ, κ³±μ…ˆ, λ‚˜λˆ—μ…ˆμ„ μˆ˜ν–‰ν•©λ‹ˆλ‹€.
  • μ‚¬μš©μžκ°€ β€˜q’λ₯Ό μž…λ ₯ν•˜λ©΄ ν”„λ‘œκ·Έλž¨μ΄ μ’…λ£Œλ©λ‹ˆλ‹€.
  • 잘λͺ»λœ μž…λ ₯에 λŒ€ν•΄ 였λ₯˜ λ©”μ‹œμ§€λ₯Ό 좜λ ₯ν•˜κ³ , λ‹€μ‹œ μž…λ ₯λ°›μŠ΅λ‹ˆλ‹€.

11.1.2 μ½”λ“œ 예제

def add(a, b):
    return a + b
 
def subtract(a, b):
    return a - b
 
def multiply(a, b):
    return a * b
 
def divide(a, b):
    if b == 0:
        return "0으둜 λ‚˜λˆŒ 수 μ—†μŠ΅λ‹ˆλ‹€."
    return a / b
 
def calculator():
    while True:
        print("계산기λ₯Ό μ’…λ£Œν•˜λ €λ©΄ 'q'λ₯Ό μž…λ ₯ν•˜μ„Έμš”.")
        num1 = input("첫 번째 숫자λ₯Ό μž…λ ₯ν•˜μ„Έμš”: ")
        if num1 == 'q':
            break
        num2 = input("두 번째 숫자λ₯Ό μž…λ ₯ν•˜μ„Έμš”: ")
        if num2 == 'q':
            break
        operator = input("μ—°μ‚°μžλ₯Ό μž…λ ₯ν•˜μ„Έμš” (+, -, *, /): ")
        if operator == 'q':
            break
 
        try:
            num1 = float(num1)
            num2 = float(num2)
        except ValueError:
            print("잘λͺ»λœ μˆ«μžμž…λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.")
            continue
 
        if operator == '+':
            print(f"κ²°κ³Ό: {add(num1, num2)}")
        elif operator == '-':
            print(f"κ²°κ³Ό: {subtract(num1, num2)}")
        elif operator == '*':
            print(f"κ²°κ³Ό: {multiply(num1, num2)}")
        elif operator == '/':
            print(f"κ²°κ³Ό: {divide(num1, num2)}")
        else:
            print("잘λͺ»λœ μ—°μ‚°μžμž…λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.")
 
if __name__ == "__main__":
    calculator()

11.1.3 ν™•μž₯ 아이디어

  • κΈ°λŠ₯ μΆ”κ°€: 제곱근, 제곱, λͺ¨λ“ˆλ‘œ μ—°μ‚° λ“±μ˜ κΈ°λŠ₯을 μΆ”κ°€ν•©λ‹ˆλ‹€.
  • GUI μΆ”κ°€: tkinter와 같은 GUI 라이브러리λ₯Ό μ‚¬μš©ν•˜μ—¬ κ³„μ‚°κΈ°μ˜ κ·Έλž˜ν”½ μΈν„°νŽ˜μ΄μŠ€λ₯Ό λ§Œλ“­λ‹ˆλ‹€.
  • 이λ ₯ 관리: μ‚¬μš©μžκ°€ μˆ˜ν–‰ν•œ 계산 이λ ₯을 μ €μž₯ν•˜κ³ , 이λ ₯을 μ‘°νšŒν•  수 μžˆλŠ” κΈ°λŠ₯을 μΆ”κ°€ν•©λ‹ˆλ‹€.

11.2 ν…μŠ€νŠΈ 기반 κ²Œμž„

ν…μŠ€νŠΈ 기반 κ²Œμž„ ν”„λ‘œμ νŠΈλŠ” κ°„λ‹¨ν•œ κ²Œμž„μ„ 톡해 파이썬의 μ œμ–΄λ¬Έ, ν•¨μˆ˜, 클래슀, 리슀트, λ”•μ…”λ„ˆλ¦¬ 등을 μ‹€μŠ΅ν•  수 μžˆλŠ” ν”„λ‘œμ νŠΈμž…λ‹ˆλ‹€. 이 κ²Œμž„μ€ ν”Œλ ˆμ΄μ–΄κ°€ νŠΉμ • λͺ…령을 μž…λ ₯ν•˜μ—¬ 캐릭터λ₯Ό μ΄λ™μ‹œν‚€κ³ , μ•„μ΄ν…œμ„ μ–»κ±°λ‚˜ 적을 λ¬Όλ¦¬μΉ˜λŠ” λ“±μ˜ ν™œλ™μ„ ν•  수 μžˆλ„λ‘ ν•©λ‹ˆλ‹€.

11.2.1 μš”κ΅¬ 사항

  • ν”Œλ ˆμ΄μ–΄λŠ” κ²Œμž„μ˜ μ‹œμž‘ μœ„μΉ˜μ—μ„œ μΆœλ°œν•˜λ©°, λ™μ„œλ‚¨λΆ λ°©ν–₯으둜 이동할 수 μžˆμŠ΅λ‹ˆλ‹€.
  • 각 μœ„μΉ˜μ—λŠ” μ•„μ΄ν…œμ΄λ‚˜ 적이 μžˆμ„ 수 있으며, ν”Œλ ˆμ΄μ–΄λŠ” μ•„μ΄ν…œμ„ μ–»κ±°λ‚˜ 적과 μ‹ΈμšΈ 수 μžˆμŠ΅λ‹ˆλ‹€.
  • ν”Œλ ˆμ΄μ–΄κ°€ κ²Œμž„μ„ 끝내기 μœ„ν•΄μ„œλŠ” λͺ©ν‘œλ₯Ό 달성해야 ν•©λ‹ˆλ‹€.
  • κ²Œμž„μ˜ μ§„ν–‰ 상황은 ν…μŠ€νŠΈλ‘œ 좜λ ₯λ©λ‹ˆλ‹€.

11.2.2 μ½”λ“œ 예제

import random
 
class Player:
    def __init__(self, name, health=100):
        self.name = name
        self.health = health
        self.inventory = []
 
    def take_damage(self, amount):
        self.health -= amount
        if self.health <= 0:
            print(f"{self.name}이(κ°€) μ‚¬λ§ν–ˆμŠ΅λ‹ˆλ‹€.")
            return False
        return True
 
    def pick_item(self, item):
        self.inventory.append(item)
        print(f"{item}을(λ₯Ό) μ£Όμ› μŠ΅λ‹ˆλ‹€!")
 
class Game:
    def __init__(self):
        self.player = Player("λͺ¨ν—˜κ°€")
        self.locations = {
            "숲": {"μ•„μ΄ν…œ": "λ¬Όμ•½", "적": "λŠ‘λŒ€"},
            "λ§ˆμ„": {"μ•„μ΄ν…œ": "μΉΌ", "적": None},
            "동꡴": {"μ•„μ΄ν…œ": None, "적": "κ±°λ―Έ"},
        }
        self.current_location = "숲"
 
    def play(self):
        while True:
            print(f"ν˜„μž¬ μœ„μΉ˜: {self.current_location}")
            location_info = self.locations[self.current_location]
            
            if location_info["μ•„μ΄ν…œ"]:
                self.player.pick_item(location_info["μ•„μ΄ν…œ"])
 
            if location_info["적"]:
                print(f"{location_info['적']}이(κ°€) κ³΅κ²©ν•©λ‹ˆλ‹€!")
                fight_result = self.battle(location_info["적"])
                if not fight_result:
                    break
 
            command = input("μ–΄λ””λ‘œ μ΄λ™ν•˜μ‹œκ² μŠ΅λ‹ˆκΉŒ? (동/μ„œ/남/뢁): ").strip().lower()
            if command == "q":
                print("κ²Œμž„μ„ μ’…λ£Œν•©λ‹ˆλ‹€.")
                break
            elif command in ["동", "μ„œ", "남", "뢁"]:
                self.move_player(command)
            else:
                print("잘λͺ»λœ λͺ…λ Ήμž…λ‹ˆλ‹€. λ‹€μ‹œ μž…λ ₯ν•˜μ„Έμš”.")
 
    def move_player(self, direction):
        possible_locations = list(self.locations.keys())
        self.current_location = random.choice(possible_locations)
        print(f"{self.current_location}으둜 μ΄λ™ν–ˆμŠ΅λ‹ˆλ‹€.")
 
    def battle(self, enemy):
        while True:
            action = input(f"{enemy}와 μ‹Έμš°κ² μŠ΅λ‹ˆκΉŒ? (예/μ•„λ‹ˆμ˜€): ").strip().lower()
            if action == "예":
                damage = random.randint(5, 20)
                print(f"{enemy}μ—κ²Œ {damage}만큼의 ν”Όν•΄λ₯Ό μž…ν˜”μŠ΅λ‹ˆλ‹€!")
                if not self.player.take_damage(damage):
                    return False
            elif action == "μ•„λ‹ˆμ˜€":
                print(f"{enemy}μ—κ²Œμ„œ λ„λ§μ³€μŠ΅λ‹ˆλ‹€!")
                return True
            else:
                print("잘λͺ»λœ λͺ…λ Ήμž…λ‹ˆλ‹€. λ‹€μ‹œ μž…λ ₯ν•˜μ„Έμš”.")
 
if __name__ == "__main__":
    game = Game()
    game.play()

11.2.3 ν™•μž₯ 아이디어

  • 지도 ν™•μž₯: κ²Œμž„ λ‚΄μ˜ 이동 κ°€λŠ₯ν•œ μž₯μ†Œλ₯Ό 더 많이 μΆ”κ°€ν•˜κ³ , 각 μž₯μ†Œλ§ˆλ‹€ νŠΉλ³„ν•œ 이벀트λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.
  • 적과 μ•„μ΄ν…œ ν™•μž₯: λ‹€μ–‘ν•œ 적과 μ•„μ΄ν…œμ„ μΆ”κ°€ν•˜μ—¬ κ²Œμž„μ˜ λ‚œμ΄λ„λ₯Ό μ‘°μ ˆν•©λ‹ˆλ‹€.
  • ν€˜μŠ€νŠΈ μΆ”κ°€: ν”Œλ ˆμ΄μ–΄κ°€ νŠΉμ • λͺ©ν‘œλ₯Ό 달성해야 ν•˜λŠ” ν€˜μŠ€νŠΈλ₯Ό μΆ”κ°€ν•˜μ—¬ κ²Œμž„μ˜ λͺ©μ μ„±μ„ κ°•ν™”ν•©λ‹ˆλ‹€.
  • μ €μž₯ κΈ°λŠ₯: ν”Œλ ˆμ΄μ–΄μ˜ μ§„ν–‰ 상황을 μ €μž₯ν•˜κ³  λ‚˜μ€‘μ— 뢈러올 수 μžˆλŠ” κΈ°λŠ₯을 μΆ”κ°€ν•©λ‹ˆλ‹€.

11.3 파일 관리 μ‹œμŠ€ν…œ

파일 관리 μ‹œμŠ€ν…œ ν”„λ‘œμ νŠΈλŠ” μ‚¬μš©μžκ°€ νŒŒμΌμ„ μ •λ¦¬ν•˜κ³  관리할 수 μžˆλŠ” κ°„λ‹¨ν•œ μ‹œμŠ€ν…œμ„ κ΅¬μΆ•ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 이 ν”„λ‘œμ νŠΈλ₯Ό 톡해 파일 μž…μΆœλ ₯, 디렉토리 관리, μ˜ˆμ™Έ 처리, μ‚¬μš©μž μž…λ ₯ 처리 등을 μ—°μŠ΅ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

11.3.1 μš”κ΅¬ 사항

  • μ‚¬μš©μžλŠ” νŠΉμ • 디렉토리 λ‚΄μ˜ νŒŒμΌμ„ μ—΄λžŒ, 볡사, 이동, μ‚­μ œν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • 디렉토리 λ‚΄μ—μ„œ νŒŒμΌμ„ 검색할 수 μžˆμŠ΅λ‹ˆλ‹€.
  • 잘λͺ»λœ μž…λ ₯에 λŒ€ν•΄ μ˜ˆμ™Έλ₯Ό μ²˜λ¦¬ν•˜κ³  였λ₯˜ λ©”μ‹œμ§€λ₯Ό 좜λ ₯ν•©λ‹ˆλ‹€.

11.3.2 μ½”λ“œ 예제

import os
import shutil
 
class FileManager:
    def __init__(self, directory):
        self.directory = directory
 
    def list_files(self):
        try:
            files = os.listdir(self.directory)
            if files:
                print("디렉토리 λ‚΄ 파일 λͺ©λ‘:")
                for file in files:
                    print(file)
            else:
                print("디렉토리가 λΉ„μ–΄ μžˆμŠ΅λ‹ˆλ‹€.")
        except FileNotFoundError:
            print("디렉토리λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
 
    def search_file(self, filename):
        try:
            files = os.listdir(self.directory)
            if filename in files:
                print(f"{filename}이(κ°€) 디렉토리에 μ‘΄μž¬ν•©λ‹ˆλ‹€.")
            else:
                print(f"{filename}을(λ₯Ό) 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
        except FileNotFoundError:
            print("디렉토리λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
 
    def copy_file(self, filename, destination):
        try:
            src_path = os.path.join(self.directory, filename)
            shutil.copy(src_path, destination)
            print(f"{filename}이(κ°€) {destination}으둜 λ³΅μ‚¬λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
        except FileNotFoundError:
            print("νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
        except IOError as e:
            print(f"파일 볡사 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
 
    def move_file(self, filename, destination):
        try:
            src_path = os.path.join(self.directory, filename)
            shutil.move(src_path, destination)
            print(f"{filename}이(κ°€) {destination}으둜 μ΄λ™λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
        except FileNotFoundError:
            print("νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
        except IOError as e:
            print(f"파일 이동 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
 
    def delete_file(self, filename):
        try:
            file_path = os.path.join(self.directory, filename)
            os.remove(file_path)
            print(f"{filename}이(κ°€) μ‚­μ œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
        except FileNotFoundError:
            print("νŒŒμΌμ„ 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.")
        except IOError as e:
            print(f"파일 μ‚­μ œ 쀑 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€: {e}")
 
def main():
    directory = input("관리할 디렉토리 경둜λ₯Ό μž…λ ₯ν•˜μ„Έμš”: ")
    manager = FileManager(directory)
 
    while True:
        print("\n파일 관리 μ‹œμŠ€ν…œ")
        print("1. 파일 λͺ©λ‘ 보기")
        print("2. 파일 검색")
        print("3. 파일 볡사")
        print("4. 파일 이동")
        print("5. 파일 μ‚­μ œ")
        print("6. μ’…λ£Œ")
        choice = input("μ›ν•˜λŠ” μž‘μ—…μ„ μ„ νƒν•˜μ„Έμš”: ")
 
        if choice == '1':
            manager.list_files()
        elif choice == '2':
            filename = input("검색할 파일 이름을 μž…λ ₯ν•˜μ„Έμš”: ")
            manager.search_file(filename)
        elif choice == '3':
            filename = input("볡사할 파일 이름을 μž…λ ₯ν•˜μ„Έμš”: ")
            destination = input("볡사할 μœ„μΉ˜λ₯Ό μž…λ ₯ν•˜μ„Έμš”: ")
            manager.copy_file(filename, destination)
        elif choice == '4':
            filename = input("이동할 파일 이름을 μž…λ ₯ν•˜μ„Έμš”: ")
            destination = input("이동할 μœ„μΉ˜λ₯Ό μž…λ ₯ν•˜μ„Έμš”: ")
            manager.move_file(filename, destination)
        elif choice == '5':
            filename = input("μ‚­μ œν•  파일 이름을 μž…λ ₯ν•˜μ„Έμš”: ")
            manager.delete_file(filename)
        elif choice == '6':
            print("ν”„λ‘œκ·Έλž¨μ„ μ’…λ£Œν•©λ‹ˆλ‹€.")
            break
        else:
            print("잘λͺ»λœ μ„ νƒμž…λ‹ˆλ‹€. λ‹€μ‹œ μ‹œλ„ν•˜μ„Έμš”.")
 
if __name__ == "__main__":
    main()

11.3.3 ν™•μž₯ 아이디어

  • λ‹€μ–‘ν•œ 파일 μž‘μ—… μΆ”κ°€: μ••μΆ• ν•΄μ œ, 파일 이름 λ³€κ²½, 디렉토리 생성 λ“±μ˜ κΈ°λŠ₯을 μΆ”κ°€ν•©λ‹ˆλ‹€.
  • μ‚¬μš©μž μΈν„°νŽ˜μ΄μŠ€ κ°œμ„ : tkinterλ₯Ό μ‚¬μš©ν•˜μ—¬ κ·Έλž˜ν”½ μ‚¬μš©μž μΈν„°νŽ˜μ΄μŠ€(GUI)λ₯Ό μΆ”κ°€ν•©λ‹ˆλ‹€.
  • 파일 μ •λ ¬ κΈ°λŠ₯: νŒŒμΌμ„ 크기, 이름, μˆ˜μ • λ‚ μ§œ λ“±μœΌλ‘œ μ •λ ¬ν•  수 μžˆλŠ” κΈ°λŠ₯을 μΆ”κ°€ν•©λ‹ˆλ‹€.
  • λ°±μ—… 및 볡ꡬ κΈ°λŠ₯: 파일의 백업을 λ§Œλ“€κ³ , ν•„μš”ν•  λ•Œ 볡ꡬ할 수 μžˆλŠ” κΈ°λŠ₯을 μΆ”κ°€ν•©λ‹ˆλ‹€.

κ²°λ‘ 

이 μž₯μ—μ„œ μ œμ‹œν•œ ν”„λ‘œμ νŠΈλ“€μ€ 파이썬의 λ‹€μ–‘ν•œ κΈ°λŠ₯듀을 μ‹€μŠ΅ν•  수 μžˆλŠ” 쒋은 κΈ°νšŒμž…λ‹ˆλ‹€. 각 ν”„λ‘œμ νŠΈλ₯Ό μˆ˜ν–‰ν•˜λ©΄μ„œ 파이썬의 기본적인 문법뢀터 κ³ κΈ‰ μ£Όμ œκΉŒμ§€ μ‹€μ œλ‘œ μ–΄λ–»κ²Œ μ μš©ν•˜λŠ”μ§€ κ²½ν—˜ν•΄λ³΄μ„Έμš”. ν”„λ‘œμ νŠΈλ₯Ό 톡해 얻은 μ‹€μŠ΅ κ²½ν—˜μ€ 파이썬 ν”„λ‘œκ·Έλž˜λ°μ„ λ”μš± 깊이 μ΄ν•΄ν•˜λŠ” 데 큰 도움이 될 κ²ƒμž…λ‹ˆλ‹€.

κ΄€λ ¨ λ¬Έμ„œ

1. 파이썬(Python) μ΄λž€