1

I can't find the error

def free_car(lane_a,lane_b,lane_c):
    cars_a = len(lane_a)
    cars_b = len(lane_b)
    cars_c = len(lane_c)

    if cars_c >= 7:
        #free_1_c
        if cars_a >= cars_b:
            #free_1_a
            #free_1_b
        else:
            #free_1_b
            #free_1_a
    elif cars_a >= cars_b:
        #free_1_a
        if cars_b > cars_c:
            #free_1_b
            #free_1_c
        else:
            #free_1_c
            #free_1_b
    elif cars_b > cars_a and cars_b > cars_c:
        #free_1_b
        if cars_a > cars_c:
            #free_1_a
            #free_1_c
        else:
            #free_1_c
            #free_1_a
    else:
        #we

The error starts on the line 11 (on the else after cars_c >= 7) I know its a stupid error and question but i can't figure it out why the error it's there

2
  • You're not indenting the code in free_car(). Commented Aug 23, 2015 at 23:01
  • sorry, the page move the tabs order, but it is idented everything Commented Aug 23, 2015 at 23:03

3 Answers 3

3

In a block like this:

if cars_a >= cars_b:
    #free_1_a
    #free_1_b
else:
    #free_1_b
    #free_1_a

Since both free_1_a and free_1_b are commented out, they are effectively empty to python. It is illegal.

You can resolve it by adding pass

i.e.

if cars_a >= cars_b:
    #free_1_a
    #free_1_b
    pass
else:
    #free_1_b
    #free_1_a
    pass

pass basically means 'no operation'

Sign up to request clarification or add additional context in comments.

Comments

0

Python requires a statement after each clause. If you want it to do nothing, simply write the word pass

if cars_c >= 7:
    #free_1_c
    if cars_a >= cars_b:
        pass
        #free_1_a
        #free_1_b
    else:
        pass
        #free_1_b
        #free_1_a
elif cars_a >= cars_b:
    #free_1_a
    if cars_b > cars_c:
        pass
        #free_1_b
        #free_1_c
    else:
        pass
        #free_1_c
        #free_1_b
elif cars_b > cars_a and cars_b > cars_c:
    #free_1_b
    if cars_a > cars_c:
        pass
        #free_1_a
        #free_1_c
    else:
        pass
        #free_1_c
        #free_1_a
else:
    pass
    #we

Comments

0

You only have comments inside those if and else blocks. That's not syntactically valid; you need an executable statement of some kind. You can use pass if there's nothing better, although in real code it's usually preferable to refactor so that the empty block isn't necessary.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.