For x in range (y) does not return right

0

I have a problem with a def x () in python. the Problem is "inside" it. Even though I put it right, it returns the else, no matter.

I've tried everything but I could not tell because it does not return right.

Here's the code.

def filters(a, b, c):
    for a in range(1, 1):
    d = "video"
    else:
    d = ""

    for b in range(1, 1):
    e = "&lclk=short"
    else:
    e = ""

    for c in range(1, 1):
    f = "&lclk=hd"
    else:
    f = ""

    i = ("&filters=%s%s%s" % (e, d, f))
    return i

Even though I put filters (1, 1, 1) it returns only the "elses". I wonder why? Ah detail that if I try for if inside the def it says "syntax error": / (it's python 2.7)

    
asked by anonymous 20.05.2015 / 01:30

1 answer

2

It will always go into else because it does not have any break .

The else for the function works as follows, if the condition you want is satisfied, exit with the break, otherwise it will execute the else , considering that the for did not match what you expected.

In addition, I believe that else , in this case, is unnecessary, since you only want to repeat an assignment once ...

What it looks like you want are ifs and not fors, I'll put the code that I imagine you want:

def filters(a, b, c):
   if a in range(1, 2):
        d = "video"
   else:
        d = ""

   if b in range(1, 2):
        e = "&lclk=short"
   else:
        e = ""

   if c in range(1, 2):
        f = "&lclk=hd"
   else:
        f = ""

   i = ("&filters=%s%s%s" % (e, d, f))
   return i

I imagine that's what you want to do. If you only want a fixed value (not a list, such as range ), replace with the following code:

def filters(a, b, c):
   if a == 1:
        d = "video"
   else:
        d = ""

   if b == 1:
        e = "&lclk=short"
   else:
        e = ""

   if c == 1:
        f = "&lclk=hd"
   else:
        f = ""

   i = ("&filters=%s%s%s" % (e, d, f))
   return i
    
20.05.2015 / 01:36