투자 공부

일복리, 월복리, 연복리 계산 파이썬 코드

경영지원실장 2023. 12. 9. 20:58
728x90
반응형

안녕하세요. 오늘은 파이썬으로 일복리, 월복리, 연복리를 계산하는 간단한 코드를 만들어보겠습니다. 

 

이 코드는 이자를 계산하는 프로그램으로, 이자 계산을 위한 간편한 도구로 활용할 수 있습니다. 

 

우선 전체 코드를 간단히 살펴보겠습니다. 

 

def calculate_simple_interest(principal, rate, time):
    # 일반적인 이자 계산 공식: 이자 = 원금 * 이자율 * 시간
    interest = principal * (rate / 100) * time
    return interest

def calculate_compound_interest(principal, rate, time, frequency):
    # 복리 이자 계산 공식: 이자 = 원금 * ((1 + 이자율 / 빈도) ** (빈도 * 시간)) - 원금
    amount = principal * ((1 + rate / (100 * frequency)) ** (frequency * time))
    interest = amount - principal
    return interest

def main():
    principal = float(input("원금을 입력하세요: "))
    rate = float(input("이자율을 입력하세요(연이율 기준, %): "))
    time = float(input("기간을 입력하세요(년): "))

    interest_type = input("이자 종류를 선택하세요 (일복리: 1, 월/연복리: 2): ")

    if interest_type == '1':
        # 일복리 계산
        simple_interest = calculate_simple_interest(principal, rate, time)
        print(f"일반 이자(일복리)는 {simple_interest:.2f} 원입니다.")
    elif interest_type == '2':
        # 복리 계산을 위한 추가 정보 입력
        frequency = int(input("이자 계산 빈도를 입력하세요(연 단위): "))
        compound_interest = calculate_compound_interest(principal, rate, time, frequency)
        print(f"복리 이자(월복리 또는 연복리)는 {compound_interest:.2f} 원입니다.")
    else:
        print("올바른 이자 종류를 선택하세요.")

if __name__ == "__main__":
    main()

 

일복리, 월복리, 연복리를 계산하는 파이썬 코드입니다. 원금, 이자율, 기간 및 이자 계산 빈도(주기)를 입력할 수 있도록 코드가 만들어져있어요. 내용을 입력한 후 코드를 실행하면 복리이자를 계산할 수 있답니다. 

 

아래는 코드 내용에 대한 설명입니다. 

def calculate_simple_interest(principal, rate, time):
# 일반적인 이자 계산 공식: 이자 = 원금 * 이자율 * 시간
    interest = principal * (rate / 100) * time
    return interest

 

  • calculate_simple_interest 함수는 주어진 원금(principal), 이자율(rate), 기간(time) 이용하여 일반적인 이자를 계산합니다.
  • 주어진 원금, 이자율, 기간을 이용하여 일복리를 계산하는 공식에 따라 이자를 계산하고 반환합니다.
def calculate_compound_interest(principal, rate, time, frequency):
# 복리 이자 계산 공식: 이자 = 원금 * ((1 + 이자율 / 빈도) ** (빈도 * 시간)) - 원금
    amount = principal * ((1 + rate / (100 * frequency)) ** (frequency * time))
    interest = amount - principal
    return interest

 

  • calculate_compound_interest 함수는 주어진 원금(principal), 이자율(rate), 기간(time), 이자 계산 빈도(frequency) 이용하여 복리 이자를 계산합니다.
  • 주어진 정보를 바탕으로 복리 이자를 계산하는 공식에 따라 이자를 계산하고 반환합니다.
def main():
    principal = float(input("원금을 입력하세요: "))
    rate = float(input("이자율을 입력하세요(연이율 기준, %): "))
    time = float(input("기간을 입력하세요(년): "))

    interest_type = input("이자 종류를 선택하세요 (일복리: 1, 월/연복리: 2): ")

    if interest_type == '1':
        # 일복리 계산
        simple_interest = calculate_simple_interest(principal, rate, time)
        print(f"일반 이자(일복리)는 {simple_interest:.2f} 원입니다.")
   
    elif interest_type == '2':
    # 복리 계산을 위한 추가 정보 입력
        frequency = int(input("이자 계산 빈도를 입력하세요(연 단위): "))
        compound_interest = calculate_compound_interest(principal, rate, time, frequency)
        print(f"복리 이자(월복리 또는 연복리)는 {compound_interest:.2f} 원입니다.")

    else:
        print("올바른 이자 종류를 선택하세요.")
   
    if __name__ == "__main__":
        main()

 

코드는 개의 함수와 main 함수로 구성되어 있습니다. calculate_simple_interest 함수는 주어진 원금, 이자율, 기간을 이용하여 일복리를 계산합니다. 반면 calculate_compound_interest 함수는 원금, 이자율, 기간, 이자 계산 빈도를 받아서 복리 이자를 계산합니다.

사용자가 실행하면 원하는 종류의 이자를 선택할 있는 메뉴가 표시됩니다. 이자 종류를 선택하고 해당하는 정보를 입력하면, 프로그램은 일복리 또는 복리 이자를 계산하여 결과를 출력합니다.

 

코드를 사용하면 쉽게 이자를 계산할 있어서 금융 계획을 세우거나 투자를 계획하는 도움이 것 같네요. 코드를 수정하면 추가적인 기능을 넣을 수도 있으니 여러분의 상황에 맞게 활용해보세요.

 

간단한 코드지만, 금융 계산을 쉽게 있는 파이썬의 강력함을 보여주는 좋은 예시라고 생각합니다. 여러분도 코드를 실행해보시고 자신만의 활용 방안을 찾아보세요!

728x90
반응형