vendredi 30 août 2019

Should I use loops or stick with nested if statements?

I am new at programming. I am using python. I am making a program that will compare different interest rates for saving accounts in my country (Paraguay). Since some of the calculations are similar in the banks, but some differ, I'd like to know how to better structure the code so that it won't repeat. Should I just keep using if statements? Is there a way to do it with loops? How should I think about it? The code I add below is just for one bank. If I wanted to add another one, how should I proceed? My natural inclination would just keep doing if statements :)

PD: If There is also any feedback on my code will be appreciated. Or some resources that you think would help me at this stage. Please be kind, I am still very insecure about all this haha.

def ahorro_cda_nominal_anual (ahorro_inicial, tiempo_en_dias, moneda):

if moneda == 'dolares':
    if ahorro_inicial>= 5000: 

        if 0 < tiempo_en_dias < 90:
            dMin = 0.05
            dMax = 0.05

        if 90 <= tiempo_en_dias < 180: 
            dMin = 0.15
            dMAx = 0.75

        if 180 <= tiempo_en_dias < 365:
            dMin = 0.25
            dMax = 1.25

        if 365 <= tiempo_en_dias:
            dMin = 1.25
            dMax = 2.00
        monto_final_min = ahorro_inicial * tiempo_en_dias*dMin/365 + ahorro_inicial
        monto_final_max = ahorro_inicial * tiempo_en_dias *dMax/365 + ahorro_inicial
        print ("Obtendrias minimamente " + str(round(monto_final_min/1000,3)) + " mil dolares.")
        print ("Hasta un valor maximo de " + str(round(monto_final_max/1000,3)) + " mil dolares.")
        return (monto_final_min, monto_final_max)

    else:
        print ("El valor minimo es de 5000 dolares. Necesitas " + str(5000 - ahorro_inicial) + " dolares mas.")

elif moneda == 'guaranies':
    if ahorro_inicial >= 5000000:

        if 0 < tiempo_en_dias < 90:
            gMin = 0.25
            gMax = 2.5

        if 90 <= tiempo_en_dias < 180: 
            gMin = 0.75
            gMax = 2.5

        if 180 <= tiempo_en_dias < 365:
            gMin = 1.0
            gMax = 4.5

        if 365 <= tiempo_en_dias:
            gMin = 1.5
            gMax = 5.5

        monto_final_min = ahorro_inicial * tiempo_en_dias*gMin/365 + ahorro_inicial
        monto_final_max = ahorro_inicial * tiempo_en_dias *gMax/365 + ahorro_inicial
        print ("Obtendras minimamente " + str(round(monto_final_min/1000000,1)) + " milllones de guaranies.")
        print ("Y a lo sumo " + str(round(monto_final_max/1000000,1)) + " millones de guaranies.")
        return (monto_final_min, monto_final_max)

    else:
        print ("El monto minimo es 5 millones de guaranies. Necesitas " + str(5000000 - ahorro_inicial) + " guaranies mas.")
else:
    print ("Solo aceptamos guaranies o dolares.")

ahorro_cda_nominal_anual (50000000, 180,'guaranies')

Aucun commentaire:

Enregistrer un commentaire