直接用了个死循环,可以计算(期末支付)年金现值、终值,复利现值、终值。把work注释掉,取消print_df的注释可以打印现值系数表。

import pandas as pd
import numpy as np
pd.set_option('display.max_columns', None)
pd.set_option('display.width', None)

def PVIFA(r, n):
    p_a = 1/r - 1/(r*(1+r)**n)
    return p_a

def PVIF(r, n):
    p_f = 1/(1+r)**n
    return p_f

def print_pa_df():
    print('=======================================================年金现值系数表============================================================')
    pa_array = np.zeros((15,15))
    for n in range(pa_array.shape[0]):
        for r in range(pa_array.shape[1]):
            pa_array[n][r] = PVIFA((r+1)/100, n+1)
    pa_array = np.around(pa_array, decimals=4)
    columns = ['{}%'.format(r+1) for r in range(pa_array.shape[1])]
    index = ['{}'.format(n+1) for n in range(pa_array.shape[0])]
    pa_df = pd.DataFrame(pa_array, index=index, columns=columns)
    print(pa_df)

def print_pf_df():
    print('=======================================================复利现值系数表=======================================================')
    pf_array = np.zeros((15,15))
    for n in range(pf_array.shape[0]):
        for r in range(pf_array.shape[1]):
            pf_array[n][r] = PVIF((r+1)/100, n+1)
    pf_array = np.around(pf_array, decimals=4)
    columns = ['{}%'.format(r+1) for r in range(pf_array.shape[1])]
    index = ['{}'.format(n+1) for n in range(pf_array.shape[0])]
    pf_df = pd.DataFrame(pf_array, index=index, columns=columns)
    print(pf_df)

def print_df():
    print_pa_df()
    print('\n\n\n\n\n')
    print_pf_df()


def caculate():
    x = input('按1计算年金系数,按2计算复利系数:')
    if x == '1':
        y = input('按1算现值,按2算年金:')
        if y == '1':
            a = float(input('输入年金:'))
            r = float(input('输入利率:'))
            n = int(input('输入期数:'))
            print('年金为{}、利率为 {}、期数为 {} 的年金现值为 {:.4f}'.format(a, r, n, a*PVIFA(r, n)))
        elif y=='2':
            a = float(input('输入现值:'))
            r = float(input('输入利率:'))
            n = int(input('输入期数:'))
            print('年金为{}、利率为 {}、期数为 {} 的现值年金为 {:.4f}'.format(a, r, n, a/PVIFA(r, n)))
        else:
            print('输入错误')
    elif x == '2':
        y = input('按1算现值,按2算终值:')
        if y == '1':
            a = float(input('输入终值:'))
            r = float(input('输入利率:'))
            n = int(input('输入期数:'))
            print('终值为{}、利率为 {}、期数为 {} 的复利现值为 {:.4f}'.format(a, r, n, a*PVIF(r, n)))
        elif y=='2':
            a = float(input('输入现值:'))
            r = float(input('输入利率:'))
            n = int(input('输入期数:'))
            print('现值为{}、利率为 {}、期数为 {} 的复利终值为 {:.4f}'.format(a, r, n, a/PVIF(r, n)))
        else:
            print('输入错误')
    else:
        print('输入错误')

def work():
    while 1:
        try:
            caculate()
        except:
            print('出现错误!')
            continue
        
# print_df()
work()