# my_module.py
def scoring(sc1, sc2, sc3, sc4, sc5):
idv_lmt, avg_lmt = 40, 60
def total_score():
tot_sc = sc1 + sc2 + sc3 + sc4 + sc5
print(f" > 총점: {tot_sc}점")
return tot_sc
def avg_score():
avg_sc = total_score() / 5
print(f" > 평균: {avg_sc}점")
return avg_sc
def print_idv_res():
print(f" > 1. {sc1}: {'Pass' if sc1 >= idv_lmt else 'Fail'}")
print(f" > 2. {sc2}: {'Pass' if sc2 >= idv_lmt else 'Fail'}")
print(f" > 3. {sc3}: {'Pass' if sc3 >= idv_lmt else 'Fail'}")
print(f" > 4. {sc4}: {'Pass' if sc4 >= idv_lmt else 'Fail'}")
print(f" > 5. {sc5}: {'Pass' if sc5 >= idv_lmt else 'Fail'}")
def print_tot_res():
if avg_score() >= avg_lmt and \
(sc1 >= idv_lmt) and \
(sc2 >= idv_lmt) and \
(sc3 >= idv_lmt) and \
(sc4 >= idv_lmt) and \
(sc5 >= idv_lmt):
print(" > 최종 합격여부: Pass")
else:
print(" > 최종 합격여부: Fail")
print_idv_res()
print_tot_res()
# run.py
import scoring as mm
if __name__ == "__main__":
sc1 = int(input("점수1 입력: "))
sc2 = int(input("점수2 입력: "))
sc3 = int(input("점수3 입력: "))
sc4 = int(input("점수4 입력: "))
sc5 = int(input("점수5 입력: "))
mm.scoring(sc1, sc2, sc3, sc4, sc5)
# run.py 실행 시
# 점수1 입력: 60
# 점수2 입력: 40
# 점수3 입력: 35
# 점수4 입력: 50
# 점수5 입력: 60
# > 1. 60: Pass
# > 2. 40: Pass
# > 3. 35: Fail
# > 4. 50: Pass
# > 5. 60: Pass
# > 총점: 245점
# > 평균: 49.0점
# > 최종 합격여부: Fail
다음과 같이 상품 구매 개수에 따라 할인율이 결정되는 프로그램을 만드시오. (단, 계산 시에는 별도의 사용자 정의 모듈을 활용하시오.)
| 구매개수 | 1개 | 2개 | 3개 | 4개 | 5개 이상 |
|---|---|---|---|---|---|
| 할인율(%) | 5 | 10 | 15 | 20 | 25 |
# my_module.py
def discount_calc(price_list):
tot_cnt = len(price_list)
if tot_cnt == 1:
rto = 0.05
elif tot_cnt == 2:
rto = 0.10
elif tot_cnt == 3:
rto = 0.15
elif tot_cnt == 4:
rto = 0.20
elif tot_cnt >= 5:
rto = 0.25
return rto
# run.py
import my_module as mm
if __name__ == "__main__":
price_list = list()
while True:
choice = int(input("원하는 기능을 선택하세요: \n> (1) 가격 입력 (2) 결제 "))
if choice == 1:
price = int(input("상품 가격을 입력하세요: "))
price_list.append(price)
elif choice == 2:
rto = mm.discount_calc(price_list)
res = int(sum(price_list) * (1-rto))
print(f"> 할인 전 금액: {sum(price_list):,d}원")
print(f"> 할인율: {int(rto*100):,d}%")
print(f"> 할인 후 금액: {res:,d}원")
break
# run.py 실행결과
# 원하는 기능을 선택하세요:
# > (1) 가격 입력 (2) 결제 1
# 상품 가격을 입력하세요: 5000
# 원하는 기능을 선택하세요:
# > (1) 가격 입력 (2) 결제 1
# 상품 가격을 입력하세요: 20000
# 원하는 기능을 선택하세요:
# > (1) 가격 입력 (2) 결제 2
# > 할인 전 금액: 25,000원
# > 할인율: 10%
# > 할인 후 금액: 22,500원
# my_module.py
def compare_res(answer_list, choice_list):
result_list = list()
for choice in choice_list:
if choice in answer_list:
result_list.append(choice)
return result_list
# run.py
import my_module as mm
import random
if __name__ == "__main__":
answer_list = list()
for i in range(6):
answer_list.append(random.randint(1, 45))
choice = input("1~45 사이의 수 6개를 입력하세요 (공백 구분): ")
choice_list = list(map(int, choice.split()))
assert len(choice_list) == 6, "6개의 숫자를 입력하여 주세요."
assert min(choice_list) >= 1 and max(choice_list) <= 45, "1~45 사이의 숫자만 입력하여 주세요."
result_list = mm.compare_res(answer_list, choice_list)
print(f"> 정답: {answer_list}")
print(f"> 선택: {choice_list}")
print("> 일치: 없음") if len(result_list) == 0 else print(f"> 일치: {result_list}")
# run.py 실행결과
# 1~45 사이의 수 6개를 입력하세요 (공백 구분): 35 43 1 3 9 28
# > 정답: [7, 27, 3, 32, 4, 31]
# > 선택: [35, 43, 1, 3, 9, 28]
# > 일치: [3]
# my_module.py
from itertools import permutations # 조합은 combinations
def get_perm(n, p):
example_list = range(1, n+1)
res_list = list(permutations(example_list, p))
return res_list
# run.py
import my_module as mm
if __name__ == "__main__":
n = int(input("n 입력: "))
p = int(input("p 입력: "))
assert n >= p, "p는 n보다 클 수 없습니다."
res_list = mm.get_perm(n, p)
print("="*40)
print(f"{n}P{p} 계산결과: {len(res_list)}")
print(f"{n}P{p} 예시조합")
for res in res_list:
print(res, end=" ")
# run.py 실행결과
# n 입력: 3
# p 입력: 2
# ========================================
# 3P2 계산결과: 6
# 3P2 예시조합
# (1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)
*이 글은 제로베이스 데이터 취업 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.