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

[복기] 파이썬 복리이자 계산

by 개발에정착하고싶다 2022. 4. 30.
320x100
# 정답코드
money = int(input('금액 입력: '))
rate = float(input('이율 입력: '))
term = int(input('기간 입력: '))

targetMoney = money

# for문에서 변수로써 활용이 되는 i를 굳이 사용하지 않더라도, term이라고 ()안에 들어간 숫자만큼
# 반복되는건 처음알게 된것같다.

for i in range(term):
    targetMoney += targetMoney * rate * 0.01

# 이것을 아래 print formating 까지 써서 넣어봤는데 에러가 계속 뜨더라;
targetMoneyFormated = format(int(targetMoney),',')

print('-'*30)
print('이율: {}%'.format(rate))
print('원금: {}원'.format(format(money,',')))
print('{}년 후 금액: {}원'.format(term,targetMoneyFormated))
print('-'*30)

 

# 이건 내가 작성한 코드로써 잘못된 코드다.

# 어디가 잘못되었냐면, 내 기준에서 복리를 구하는 원리금 계산식이 작동이 안되더라;

price = float(input('금액 입력: '))
rate = float(input('이율 입력: ')) * 0.01
period = float(input('기간 입력(연): '))
interst = 0


#  복리계산 공식
# 원리금 = 원금(1+연이율/복리 횟수)복리횟수*기간

interst = (price(1+rate)) * period

print('-'*30)
print(f'이율: {rate}%')
print('원금: {}원'.format(format(price,',')))
print(interst)
print('-'*30)

결국, 복리 원리금을 계산하는 핵심코드는 정답코드 기준으로

for i in range(term):
    targetMoney += targetMoney * rate * 0.01

였다.

 

그런데 내 코드가 작동이 왜 안되는지는 아직도 미지수라서. 스스로 연구를 해봐야겠다.

300x250