[Solved] How to avoid used many if statements in definition?


Use a for loop to iterate over the months:

def prepareadres(city,y,m,d):
    months = [31,28,31,30,31,30,31,31,30,31,30,31]
    d = d + 1
    d0 = 0
    m0 = 0
    for month in months:
        if d > month:
             d = d - month
             m += 1
    if d == sum(months):    
        print("year complete")
        #print(df.iloc[0, :-2])
    if (m < 10) and (d < 10):
        adres = "https://sinoptik.ua/погода-" + city + "https://stackoverflow.com/" + str(y) + "-" + str(m0) + str(m) + "-" + str(d0) + str(d)
    elif (m < 10) and (d >= 10):
        adres = "https://sinoptik.ua/погода-" + city + "https://stackoverflow.com/" + str(y) + "-"+ str(m0) + str(m) + "-" + str(d)
    elif (m >= 10) and (d < 10):
        adres = "https://sinoptik.ua/погода-" + city + "https://stackoverflow.com/" + str(y) + "-" + str(m) + "-" + str(d0) + str(d)   
    else:
        adres = "https://sinoptik.ua/погода-" + city + "https://stackoverflow.com/" + str(y) + "-" + str(m) + "-" + str(d)
    #print(adres)
    return adres

1

solved How to avoid used many if statements in definition?