Home > Back-end >  How to use loop in this case inside the function?
How to use loop in this case inside the function?

Time:05-07

def get_day_type(info):                                                   
    
    day_type = (info[info.find("(") 1:info.find(")")])

    holiday = ["Sun", "Sat"]                              
    if day_type not in holiday:
        day_type = ("weekday")
        return day_type
        
    else:
        day_type = ("weekend")
        return day_type

The problem that it´s printing the type of the first day only and ignore the rest. for example:

print(get_day_type("Normal: 2May2022(mon), 3May2022(tues), 18Mar2021(Sun)"))

it only prints:

weekday

But I want my output to print all types of days. for example :

weekday

weekday

weekend

Note: I need to follow the same format of info input:

Which is ==>clientType: 12May1995(mon), 10May1995(tues), ........etc

CodePudding user response:

I think this is what you are looking for.

def get_day_type(info):                                                   
    values = info.split(",")
    output = []
    for value in values:
        day_type = (value[value.find("(") 1:value.find(")")])
    
        holiday = ["Sun", "Sat"]                              
        if day_type not in holiday:
            day_type = ("weekday")
            output.append(day_type)
            
        else:
            day_type = ("weekend")
            output.append(day_type)

    return output

print(get_day_type("Normal: 2May2022(mon), 3May2022(tues), 18Mar2021(Sun)"))

output is

weekday
weekday
weekend

It just splits the info on the commas. It then iterates through the list created and uses the code you had to get the weekday or weekend for each item in the list. The outputs are stored in a list and then returned.

  • Related