티스토리 뷰

Python

모듈 사용예제(영화표 가격)

Programmers 2021. 4. 17. 13:49
반응형

나도코딩님의 강의 중 예제 공부하면서 간단한 모듈 설명에 대해서 기록한다.
다음에 분명히 까먹을거니깐 미리 저장해둔다 머리가 좋지 않기에...

- 파일명(theater_module.py) -> 모듈로 안에 함수들을 정의해놨다.
--------------------------------------------------------------
#영화가격 모듈

#일반가격
def price(people):
    print("{0}명 가격 {1}".format(people, people*10000))

#조조가격
def price_morning(people):
    print("{0}명 가격 {1}".format(people, people*6000))

#군인가격
def price_solider(people):
    print("{0}명 가격 {1}".format(people, people*4000))
--------------------------------------------------------------



- 파일명(theater_module_play.py) -> 이 파일에서 실행하면 되는데 기억안날때 방법별로 보는걸 추천!
그리고 파일은 꼭 위의 theater_module.py랑 theater_module_play.py가 같은 위치에 있어야 된다! 
이 코드들 한번에 다 실행시키면 안된다 따로 따로 해봐야한다 제발.
--------------------------------------------------------------
#모듈을 사용하는 방법1
import theater_module
theater_module.price(5)
theater_module.price_morning(3)
theater_module.price_solider(2)
--------------------------------------------------------------
#모듈을 사용하는 방법2
import theater_module as tm #별명tm으로 사용가능
tm.price(5)
tm.price_morning(3)
tm.price_solider(2)
--------------------------------------------------------------
#모듈을 사용하는 방법3
from theater_module import * #여기에서 함수를 입력한 것 처럼 모두 사용하겠다고 명시
price(5)
price_morning(2)
price_solider(3)
--------------------------------------------------------------
#모듈을 사용하는 방법4
from theater_module import price, price_morning #theater_module에서 price와 price_morning만 쓸거야
price(5)
price_morning(3)
# price_solider(1) #이건 못쓰지 위에 import뒤에 명시 안했자나
--------------------------------------------------------------
#모듈을 사용하는 방법5
from theater_module import price_solider as price #price_solider을 가져와서 쓸건데, 별명을 price로 바꿔서 쓸래 
price(5# = 일반가격이 아니라 여기서는 별명인 price_solider로 쓰임.

요로캐 주석처리해가면서 보자!

자 기본적인 모듈사용법은 요로케....
이제 모듈을 만들어놓고 가져다 쓰면 된다!
자~ 공부하자 공부!

반응형
댓글
공지사항