본문 바로가기
개발일지/Python

[복기] 인원 카운트, 개별금액, 총금액 영수증 출력하기

by 개발에정착하고싶다 2022. 5. 5.
320x100
# 다듬지 않은 내 코드
# 결과는 성공하긴 했는데 코드를 만드는 시간이 15분정도 걸렸다.

def cal():

    childPrice = 18000
    infatPrice = 25000
    adultPrice_over12 = 50000
    handicap = 0.5

    a = int(input('유아 입력: '))
    b = int(input('할인대상 유아 입력: '))
    c = int(input('소아 입력: '))
    d = int(input('할인대상 소아 입력: '))
    e = int(input('성인 입력: '))
    f = int(input('할인대상 성인 입력: '))

    cp = a * childPrice
    hcp = b * childPrice * handicap
    ip = c * infatPrice
    hip = d * infatPrice * handicap
    ap = e * adultPrice_over12
    hap = f * adultPrice_over12 * handicap

    print('='*60)
    print('유아 {}명 요금: {}원'.format(a, format(cp, ',')))
    print('유아 할인 대상 {}명 요금: {}원'.format(b, format(hcp, ',')))
    print('소아 {}명 요금: {}원'.format(c, format(ip, ',')))
    print('소아 할인 대상 {}명 요금: {}원'.format(d, format(hip, ',')))
    print('성인 {}명 요금: {}원'.format(e, format(ap, ',')))
    print('성인 할인 대상 {}명 요금: {}원'.format(f, format(hap, ',')))

    print('=' * 60)
    print('Total: {}명'.format(a+b+c+d+e+f))
    print('TotlaPrice: {}원'.format(format(cp+hcp+ip+hip+ap+hap),','))
    print('=' * 60)

cal()

 

# 답안 코드

childPrice = 18000
infantPrice = 25000
adultPrice = 50000
specialDC = 0.5

# 이건 금액 다듬어 줄때 천자리 구분 포멧팅과 같은 위치에 써주면 된다.

def formatedNumber(n):
    return format(n, ',')

def printAirPlaneReceipt(c1, c2, i1, i2, a1, a2):
    cp = c1 * childPrice
    cp_dc = int(c2 * childPrice *specialDC)
    print(f'유아 {c1}명 요금: {cp}원 ')
    print(f'유아 할인 대상 {c2}명 요금: {cp_dc}원 ')

    ip = i1 * infantPrice
    ip_dc = int(i2 * infantPrice * specialDC)
    print(f'소아 {i1}명 요금: {ip}원 ')
    print(f'소아 할인 대상 {i2}명 요금: {ip_dc}원 ')

    ap = a1 * adultPrice
    ap_dc = int(a2 * adultPrice * specialDC)
    print(f'성인 {a1}명 요금: {ap}원 ')
    print(f'성인 할인 대상 {a2}명 요금: {ap_dc}원 ')

    print(f'Total: {c1 + c2 + i1 + i2 + a1 + a2}명')
    print(f'TotalPrice: {cp + cp_dc + ip + ip_dc + ap + ap_dc}원')

childCnt = int(input('유아 입력: '))
specailDcChildCnt = int(input('할인 대상 유아 입력: '))

infantCnt = int(input('소아 입력: '))
specailDcInfantCnt = int(input('할인 대상 소아 입력: '))

adultCnt = int(input('성인 입력: '))
specailDcAdultCnt = int(input('할인 대상 성인 입력: '))

printAirPlaneReceipt(childCnt, specailDcChildCnt, infantCnt, specailDcInfantCnt, adultCnt, specailDcAdultCnt)

# 코드 줄의 길이는 서로 비슷한데
# 느낌탓인가? 후자의 코드가 더 가독성도 좋은것같고 수정하기에도 헷갈림이 덜한것같다
# 뭔가 "클린 코드" 이런걸 배워야할것같다
300x250