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)λ₯Ό μΆκ°ν©λλ€. - νμΌ μ λ ¬ κΈ°λ₯: νμΌμ ν¬κΈ°, μ΄λ¦, μμ λ μ§ λ±μΌλ‘ μ λ ¬ν μ μλ κΈ°λ₯μ μΆκ°ν©λλ€.
- λ°±μ λ° λ³΅κ΅¬ κΈ°λ₯: νμΌμ λ°±μ μ λ§λ€κ³ , νμν λ 볡ꡬν μ μλ κΈ°λ₯μ μΆκ°ν©λλ€.
κ²°λ‘
μ΄ μ₯μμ μ μν νλ‘μ νΈλ€μ νμ΄μ¬μ λ€μν κΈ°λ₯λ€μ μ€μ΅ν μ μλ μ’μ κΈ°νμ λλ€. κ° νλ‘μ νΈλ₯Ό μννλ©΄μ νμ΄μ¬μ κΈ°λ³Έμ μΈ λ¬Έλ²λΆν° κ³ κΈ μ£Όμ κΉμ§ μ€μ λ‘ μ΄λ»κ² μ μ©νλμ§ κ²½νν΄λ³΄μΈμ. νλ‘μ νΈλ₯Ό ν΅ν΄ μ»μ μ€μ΅ κ²½νμ νμ΄μ¬ νλ‘κ·Έλλ°μ λμ± κΉμ΄ μ΄ν΄νλ λ° ν° λμμ΄ λ κ²μ λλ€.