[Python] How to calculate more than 12 months [closed]

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    Please, help me to improve my three digits! I have hard issues to improve this for scientifc calculation without around odd numbers. To calculate the shortage spread over different periods, you can use the following formula:

    [ \text{Monthly Shortage} = \frac{\text{Total Shortage}}{\text{Number of Months}} ]


    Let's break down the examples you provided:
    1. **24-month shortage spread (6/29/2023)**:

    \[ \text{Monthly Shortage} = \frac{5073.72}{24} = 211.41 \text{ per month} \]

    1. 15-month shortage spread (2/14/2024):

    \[ \text{Monthly Shortage} = \frac{2704.60}{15} = 180.31 \text{ per month} \]

    1. 12-month shortage spread (6/17/2024):
      • Initially, the shortage was spread over 15 months, but 3 months were already paid, leaving 12 months.

    \[ \text{Remaining Shortage} = 3186.22 \]
    \[ \text{Monthly Shortage} = \frac{3186.22}{12} = 265.52 \text{ per month} \]


    def calculate_monthly_shortage(total_shortage, months):
    """
    Calculate the monthly shortage given the total shortage amount and the number of months.

    Parameters:
    total_shortage (float): The total shortage amount.
    months (int): The number of months over which the shortage is spread.

    Returns:
    float: The monthly shortage amount.
    """
    return total_shortage / months

    Example usage



    total_shortage_24_months = 5073.72 months_24 = 24 monthly_shortage_24 = calculate_monthly_shortage(total_shortage_24_months, months_24) print(f"Monthly shortage for 24 months: {monthly_shortage_24:.2f} per month")



    total_shortage_15_months = 2704.60 months_15 = 15 monthly_shortage_15 = calculate_monthly_shortage(total_shortage_15_months, months_15) print(f"Monthly shortage for 15 months: {monthly_shortage_15:.2f} per month")




    total_shortage_12_months = 3186.22 months_12 = 12 monthly_shortage_12 = calculate_monthly_shortage(total_shortage_12_months, months_12) print(f"Monthly shortage for 12 months: {monthly_shortage_12:.2f} per month")

    Continue reading...

Compartilhe esta Página