용자누나 탐구생활 777

[ Python ] 예외 처리(try, except, Exception, as error 등)

# 예외 : 어떤 에러가 발생했을 때, 처리해주는 것. # 에러 : 실책, 실수, 잘못된 것을 입력했을 때 # 예시) 택새 기사님이 11층 배송지로 이동하는데 그 아파트는 10층까지 밖에 없을 때...등 # URL 잘못 적거나, 숫자를 입력해야 하는데 문자열을 입력했을 때. # 예시 문제) 나누기 전용 계산기 프로그램. # 방법 1. try: print("나누기 전용 계산기입니다.") num1 = int(input("첫 번째 숫자를 입력해주세요 : ")) num2 = int(input("두 번째 숫자를 입력해주세요 : ")) print("{0} / {1} = {2}".format(num1, num2, int(num1/num2))) # 실수 출력하지 않기 위해서 int() 형변환 except ValueEr..

Python 2021.03.07

[ Python ] Quiz) 주어진 코드를 활용하여 부동산 프로그램을 작성하시오.

# Quiz) 주어진 코드를 활용하여 부동산 프로그램을 작성하시오. # (출력 예제) # 총 3대의 매물이 있습니다. # 강남 아파트 매매 10억 2010년(준공) # 마포 오피스텔 전세 5억 2007년 # 송파 빌라 월세 500/50 2000년 # [코드] class House: # 매물 초기화 def __init__(self, location, house_type, deal_type, price, completion_year): self.location = location self.house_type = house_type self.deal_type = deal_type self.price = price self.completion_year = completion_year # 매물 정보 표시 def ..

Python 2021.03.07

[ Python ] 스타크래프트 후반전

# 스타크래프트 전반전 # 텍스트 기반으로 실제 게임을 하는 것처럼 프로젝트 만들기. from random import randint # 일반 유닛 class Unit: def __init__(self, name, hp, speed=0): self.name = name self.hp = hp self.speed = speed print("{0} 유닛이 생성되었습니다.".format(self.name)) def move(self, location): print("{0} : {1} 방향으로 이동합니다. [속도 {2}]".format(self.name, location, self.speed)) def damaged(self, damage): print("{0} : {1} 데미지를 입었습니다.".format(s..

Python 2021.03.07

[ Python ] 스타크래프트 전반전

# 스타크래프트 전반전 # 텍스트 기반으로 실제 게임을 하는 것처럼 프로젝트 만들기. # 일반 유닛 class Unit: def __init__(self, name, hp, speed=0): self.name = name self.hp = hp self.speed = speed print("{0} 유닛이 생성되었습니다.".format(self.name)) def move(self, location): print("[지상 유닛 이동]") print("{0} : {1} 방향으로 이동합니다. [속도 {2}]".format(self.name, location, self.speed)) def damaged(self, damage): print("{0} : {1} 데미지를 입었습니다.".format(self.nam..

Python 2021.03.07

[ Python ] super()

# super # 일반 유닛 class Unit: def __init__(self, name, hp, speed=0): self.name = name self.hp = hp self.speed = speed def move(self, location): print("[지상 유닛 이동]") print("{0} : {1} 방향으로 이동합니다. [속도 {2}]".format(self.name, location, self.speed)) # 공격 유닛 class AttackUnit(Unit): def __init__(self, name, hp, damage, speed=0): Unit.__init__(self, name, hp, speed) # 부모에게 값을 넘겨주어 초기화하는 작업. self.damage = d..

Python 2021.03.07

[ Python ] pass

# pass # 일반 유닛 class Unit: def __init__(self, name, hp, speed): self.name = name self.hp = hp self.speed = speed def move(self, location): print("[지상 유닛 이동]") print("{0} : {1} 방향으로 이동합니다. [속도 {2}]".format(self.name, location, self.speed)) # 공격 유닛 class AttackUnit(Unit): def __init__(self, name, hp, damage, speed=0): Unit.__init__(self, name, hp, speed) # 부모에게 값을 넘겨주어 초기화하는 작업. self.damage = dama..

Python 2021.03.07

[ Python ] 메소드 오버라이딩

# 메소드 오버라이딩 # 일반 유닛 class Unit: def __init__(self, name, hp, speed): self.name = name self.hp = hp self.speed = speed def move(self, location): print("[지상 유닛 이동]") print("{0} : {1} 방향으로 이동합니다. [속도 {2}]".format(self.name, location, self.speed)) # 상속받을 때 '클래스명(상속받을 클래스명)' # 공격 유닛 class AttackUnit(Unit): def __init__(self, name, hp, damage, speed=0): Unit.__init__(self, name, hp, speed) # 부..

Python 2021.03.07

[ Python ] 다중 상속.

# 다중 상속 : 부모 클래스를 여러개 상속 받는. # 일반 유닛 class Unit: def __init__(self, name, hp): self.name = name self.hp = hp # 상속받을 때 '클래스명(상속받을 클래스명)' # 공격 유닛 class AttackUnit(Unit): def __init__(self, name, hp, damage): Unit.__init__(self, name, hp) # 부모에게 값을 넘겨주어 초기화하는 작업. self.damage = damage def attack(self, location): print("{0} : {1} 방향으로 적군을 공격 합니다. [공격력 : {2}]".format( self.name, location, self..

Python 2021.03.07

[ Python ] 상속

# 상속 # 일반 유닛 class Unit: def __init__(self, name, hp): self.name = name self.hp = hp # 상속받을 때 '클래스명(상속받을 클래스명)' # 공격 유닛 class AttackUnit(Unit): def __init__(self, name, hp, damage): Unit.__init__(self, name, hp) # 부모에게 값을 넘겨주어 초기화하는 작업. self.damage = damage def attack(self, location): print("{0} : {1} 방향으로 적군을 공격 합니다. [공격력 : {2}]".format( self.name, location, self.damage )) def damaged(s..

Python 2021.03.07

[ Python ] 메소드

# 메소드 class Unit: def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage print("{0} 유닛이 생성 되었습니다.".format(self.name)) print("체력 {0}, 공격력 {1}".format(self.hp, self.damage)) # 공격 유닛 class AttackUnit: def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage def attack(self, location): print("{0} : {1} 방향으로 적군을 공격 합니다. [공격력 : {2}]".fo..

Python 2021.03.07

[ Python ] 멤버 변수

클래스 외부에서 원하는 변수 확장 확장된 변수는 확장한 객체에 대해서만 적용되고 다른 객체에 대해서는 적용되지 않는다. # 클래스 만들기 # __init__ : Python에서 사용되는 생성자 # marine이나 tank 같은 객체가 만들어질 때 자동으로 호출되는 부분. # 객체 : 클래스에 의해 만들어진. # marine, tank는 Unit 클래스의 '인스턴스'라고 한다. # 멤버 변수 : 클래스 내에서 정의된 변수(self.name/self.hp/self.damage) # ------------ 클래스 생성 시작 -------------- class Unit: def __init__(self, name, hp, damage): self.name = name self.hp = hp se..

Python 2021.03.07

[ Python ] 클래스 생성자 선언

# 클래스 만들기 # __init__ : Python에서 사용되는 생성자 # marine이나 tank 같은 객체가 만들어질 때 자동으로 호출되는 부분. # 객체 : 클래스에 의해 만들어진. # marine, tank는 Unit 클래스의 '인스턴스'라고 한다. # ------------ 클래스 생성 시작 -------------- class Unit: def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage print("{0} 유닛이 생성 되었습니다.".format(self.name)) print("체력 {0}, 공격력 {1}".format(self.hp, self.damage)) # ---..

Python 2021.03.07

[ Python ] 클래스 만들기

# 클래스 만들기 # ------------ 클래스 생성 시작 -------------- class Unit: def __init__(self, name, hp, damage): self.name = name self.hp = hp self.damage = damage print("{0} 유닛이 생성 되었습니다.".format(self.name)) print("체력 {0}, 공격력 {1}".format(self.hp, self.damage)) # ------------ 클래스 생성 끝 -------------- marine1 = Unit("마린", 40, 5) marine2 = Unit("마린", 40, 5) tank = Unit("탱크", 150, 35)

Python 2021.03.07

[ Python ] 클래스 시작하기.

# 클래스 # 스타크래프트 게임 예시 # 마린 : 공격 유닛, 군인. 총을 쏠 수 있음 name = "마린" # 유닛의 이름 hp = 40 # 유닛의 체력 damage = 5 # 유닛의 공격력 print("{0} 유닛이 생성되었습니다.".format(name)) print("체력 {0}, 공격력 {1}\n".format(hp, damage)) # 탱크 : 공격 유닛, 탱크. 포를 쏠 수 있는데, 일반 모드 / 시즈 모드 tank_name = "탱크" tank_hp = 150 tank_damage = 35 print("{0} 유닛이 생성되었습니다.".format(tank_name)) print("체력 {0}, 공격력 {1}\n".format(tank_hp, tank_damage)) tank2_name = ..

Python 2021.03.07

[ Python ] 1주차부터 5주차까지의 보고서 파일을 만드는 프로그램을 작성하시오.

# Quiz) 당신의 회사에서는 매주 1회 작성해야 하는 보고서가 있습니다. # 보고서는 항상 아래와 같은 형태로 출력되어야 합니다. # - X 주차 주간보고 - # 부서 : # 이름 : # 업무 요약 : # 1주차부터 5주차까지의 보고서 파일을 만드는 프로그램을 작성하시오. # 조건 : 파일명은 '1주차.txt', '2주차.txt', ...와 같이 만듭니다. # ------------------ 풀이 시작 ------------------# for i in range(1, 6): with open(str(i) + "주차.txt", "w", encoding="utf8") as report_file: report_file.write("- {0} 주차 주간보고 -".format(..

Python 2021.03.07

[ Python ] with로 더 수월하게 파일 쓰고, 불러오기

# with : 파일 쓰기, 읽기를 조금 더 수월하게 할 수 있는 # pickle, with를 사용해서 파일 불러오기 # ----------- 방법 1 시작 ------------- import pickle # with로 파일을 읽어온다. # as profile_file : 파일을 열어서 profile_file이라는 변수에 저장. with open("profile.pickle", "rb") as profile_file: print(pickle.load(profile_file)) # 출력 : {'이름': '박명수', '나이': 30, '취미': ['축구', '골프', '코딩']} # with 문을 수행하고 ..

Python 2021.03.07

[ Python ] pickle 라이브러리(파일을 바이너리 형태로 쓰고, 불러오기)

# pickle : 프로그램 상에서 우리가 사용하고 있는 데이터를 파일 형태로 저장해주는. # 파일을 누군가에게 주면 그 분이 파일을 열어서 pickle 이용해서 데이터를 가지고 와서 # 코드에서 또 사용할 수 있는. # pickle을 사용하려면 import 해야 한다. # 우선 pickle 이라는 module을 import # pickle을 이용해서 파일을 쓰고, 불러올 수 있는 유용한 라이브러리. import pickle # ----------------- pickle 파일 작성 시작 ------------------ # 일단 파일에다가 저장을 할 것이다. # 파일 타입은 pickle이고, "write" 목적이며 b를 붙여준다. # b : 바이너리 의미. # pickle을 사용하려면 꼭 바이너리 타입..

Python 2021.03.07

[ Python ] 파일 입출력(읽기)

# 파일 입출력(읽기) # "r" : Read # read() : 파일의 모든 내용을 읽어온다. # 읽기 또한 파일을 close() 해준다. # ---------- 방법 1 시작 --------- score_file = open("score.txt", "r", encoding="utf8") print(score_file.read()) score_file.close() # ---------- 방법 1 끝 --------- # 파일의 모든 내용이 아닌 # '한줄 한줄' 읽어와서 무언가를 처리하고 싶을 때 # 파일은 역시 open()하고 close()해준다. # 줄바꿈 안하고 공백으로 출력하고 싶다면 end="" 사용 # -------- 방법 2 시작 --------- score_file = o..

Python 2021.03.07

[ Python ] 파일 입출력 (쓰기)

# 파일 입출력 # "w" : 파일을 write 쓰기 용도로 열기 # 이미 존재하는 파일에 "w"로 open하면 덮어쓰기가 된다. # encoding="utf8" 을 입력하지 않으면 한글이 깨질 수 있다. # open() 파일을 열어주면 필히 close()로 파일을 닫아주어야 한다. # --------- 방법1 시작------------ # score_file = open("score.txt", "w", encoding="utf8") # print("수학 : 0", file=score_file) # print("영어 : 40", file=score_file) # score_file.close() # --------- 방법1 끝------------ # "a" : 파일을 덮어쓰지 않고 이어서 쓰고 싶을 때..

Python 2021.03.07
728x90
반응형
LIST