def solution(price):
    answer = price
    
    if price>=500000:
        answer= int(price*0.8)
    elif price>=300000:
        answer=int(price*0.9)
    elif price>=100000:
        answer=int(price*0.95)
    
    return answer

<aside> 💡

처음엔 if를 이용해서 풀었다.

또한 % 의 문제의 경우 100% 에서 해당 할인율을 뺴서 나온 것을 다시 소수점으로 바꾼 값으로 곱하면 된다.

그러나 이 문제를 전에 배웠던 ~~튜플을~~ 딕셔너리를 이용해서 key, value 형태로 풀수 도 있었다.

이것이 파이썬 답고 더 공부가 되는 것 같다.

</aside>

def solution(price):
    discount_rate_tuple = {500000 : 0.8, 300000 : 0.9, 100000 : 0.95, 0 : 1}
    for total, rate in discount_rate_tuple.items():
        if price >= total:
            return price * rate

<aside> 💡

아직 정확히 딕셔너리, 튜플에 대해서 잘 숙지하지 못했다.

변수의 이름이 tuple..

다시 한번 개념을 알고 가자.

</aside>

2️⃣ 튜플 버전

def solution(price):
    # (가격 기준, 할인율) 튜플의 리스트
    discount_rate = [(500000, 0.8), (300000, 0.9), (100000, 0.95), (0, 1)]

    for limit, rate in discount_rate:
        if price >= limit:
            return int(price * rate)