jeudi 16 novembre 2017

For Loop with If statement onlyreturns first element

I am trying to feed a vector into a function which checks every element and assigns the corresponding element from the list to the proper if statement.

The output should look like this:

10 11 17 40 370

Somehow I am only receiving the first element

370

and the loop is breaking up.The code looks like the following:

def dosomething(thelist):
    period_unit = []
    for period in thelist:
        period = period.lower()        
        if period[-1:] == "d":
            period_unit = 1
        elif period[-1:] == "m":
            period_unit = 30
        elif period[-1:] == "w":
            period_unit = 7
        elif period[-1:] == "y":
            period_unit = 360
        elif period == "dummy":
            period_object = 0
            return period_object
        else:
            raise Exception("Something went wrong")
        period_object = 10 + period_unit
    return period_object   

if __name__ == '__main__':

    date = ["dummy", "1d", "1W", "1M", "1Y"]

    test = dosomething(date )
    print(test)

Aucun commentaire:

Enregistrer un commentaire