mercredi 20 février 2019

Dynamic while loop

I have a user input as a tuple list which shows the minimum and maximum profit with the percentage profit within that range, as the following tuple list shows:

args_tuple = (("min1" , 0), ("max1", 10000) , ("percentage1", 0.1),
              ("min2" , 10000) , ("max2", 50000), ("percentage2", 0.05) ,
              ("min3" , 50000), ("max3",100000), ("percentage3",0.1) ,
              ("min4" , 100000), ("max4",None), ("percentage4",0.05))

I made a simple function to calculate the profit according to that percentage as the following code shows:

def profitCalculator(summed, args_tuple):
    profit = 0

    args_tuple = dict(args_tuple)

    if args_tuple['min1'] <= summed < args_tuple['max1']:
        profit += (summed * args_tuple['percentage1'])
    elif args_tuple['min2'] <= summed < args_tuple['max2']:
        profit += (args_tuple['min2'] * args_tuple['percentage1'])
        summed -= args_tuple['min2']
        profit += (summed * args_tuple['percentage2'])
    elif args_tuple['min3'] <= summed < args_tuple['max3']:
        profit += (args_tuple['min2'] * args_tuple['percentage1'])
        profit += ((args_tuple['min3'] - args_tuple['min2']) *  args_tuple['percentage2'])
        summed -= args_tuple['min3']
        profit += (summed * args_tuple['percentage3'])
    elif summed >= args_tuple['min4']:
        profit += (args_tuple['min2'] * args_tuple['percentage1'])
        profit += ((args_tuple['min3'] - args_tuple['min2']) * args_tuple['percentage2'])
        profit += ((args_tuple['min3']) * args_tuple['percentage3'])
        summed -= args_tuple['min4']
        profit += (summed * args_tuple['percentage4'])
    return profit

What I need now is, instead of expanding if statements, I just need a dynamic while loop in case the user adds ("min5..." , 100000), ("max5...", 200000) , ("percentage5...", 0.1), with the same profit calculations showed in my function.

Aucun commentaire:

Enregistrer un commentaire